PHPackages                             igorcrevar/icrouter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. igorcrevar/icrouter

ActiveLibrary[Framework](/categories/framework)

igorcrevar/icrouter
===================

Different approach for standard routing problem. Matching tree based.

05PHP

Since Apr 16Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/igorcrevar/icRouter)[ Packagist](https://packagist.org/packages/igorcrevar/icrouter)[ RSS](/packages/igorcrevar-icrouter/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

PathForge Router
================

[](#pathforge-router)

A tree-based URL router for PHP. Instead of iterating over a list of regular expressions, PathForge Router builds a matching tree from registered routes, providing efficient O(depth) lookups.

Requires PHP 5.5 or later.

Installation
------------

[](#installation)

```
composer require pathforge/icrouter

```

Usage
-----

[](#usage)

### Setup

[](#setup)

```
use PathForge\icRouter\Router;
use PathForge\icRouter\Route;
use PathForge\icRouter\Interfaces\DefImpl\DefaultNodeBuilder;

$router = new Router(new DefaultNodeBuilder());
```

### Define routes

[](#define-routes)

```
$router->setRoutes([
    new Route('simple', '/simple',
              array('module' => 'simple')),
    new Route('simple_param', '/param/:a',
              array('module' => 'simple_param', 'a' => 10),
              array('a' => '\d+')),
    new Route('two_params', '/param/hello/:a/some/:b',
              array('module' => 'two_params', 'a' => 10, 'onemore' => 'time')),
    new Route('two_params_any', '/home/hello/:a/:b/*',
              array('module' => 'two_params_any', 'a' => 10, 'b' => '10'),
              array('b' => '[01]+')),
    new Route('complex_param', '/complex/id_:id',
              array('module' => 'complex_param'),
              array('id' => '\d+')),
    new Route('home', '/*',
              array('module' => 'home')),
]);

$router->build();
```

### Match a URL

[](#match-a-url)

```
$result = $router->match('/param/20');
// Returns: array('module' => 'simple_param', 'a' => '20')

$result = $router->match('/nonexistent');
// Returns: false
```

`match()` returns an associative array of parameters on success, or `false` if no route matches.

### Generate a URL

[](#generate-a-url)

```
$result = $router->generate('two_params', array('b' => 'aabb'));
// Returns: '/param/hello/10/some/aabb'
```

The first argument is the route name, the second is an array of parameters. Missing parameters are filled from defaults.

Route constructor
-----------------

[](#route-constructor)

```
new Route($name, $pattern, $defaults = array(), $parameters = array())
```

ArgumentDescription`$name`Unique route name, used for URL generation`$pattern`URL pattern with optional parameters and wildcard`$defaults`Default values for parameters (key-value pairs)`$parameters`Regex constraints for named parameters### Pattern syntax

[](#pattern-syntax)

- Static segments match literally: `/account/list`
- Named parameters are prefixed with `:` and match a single segment: `/account/:id`
- Named parameters can be embedded in a segment: `/account/id_:id`
- A trailing `*` captures remaining segments as key-value pairs: `/account/:id/*`
- Parameter names must match `[A-Za-z0-9]+`
- Only one named parameter is allowed per segment

### Parameter constraints

[](#parameter-constraints)

Regex patterns (without delimiters) can be specified per parameter:

```
array('id' => '\d+')            // id must be an integer
array('type' => 'car|boat|plane') // type must be one of these values
```

Route ordering
--------------

[](#route-ordering)

Routes are matched in registration order. When multiple routes could match a URL, the **first registered route wins**. Place more specific routes before general ones:

```
$router->setRoutes([
    new Route('specific', '/param/:a', ...),  // checked first
    new Route('catchall', '/*', ...),          // fallback
]);
```

HTTP methods
------------

[](#http-methods)

icRouter matches URL paths only and does not handle HTTP methods (GET, POST, PUT, DELETE, etc.). Method dispatch can be handled by your application code, for example by including the method in the route defaults:

```
$router->setRoutes([
    new Route('create_user', '/user/create',
              array('module' => 'user', 'action' => 'create', 'method' => 'POST')),
]);

$result = $router->match('/user/create');
if ($result && $result['method'] !== $_SERVER['REQUEST_METHOD']) {
    // return 405 Method Not Allowed
}
```

Performance tip
---------------

[](#performance-tip)

`$router->build()` constructs the matching tree and is relatively expensive. In production, cache the built router instance (e.g. via `serialize()`, APC, or similar) to avoid rebuilding on every request.

Unit testing
------------

[](#unit-testing)

```
vendor/bin/phpunit test/RouterTest.php

```

Benchmarks
----------

[](#benchmarks)

Run benchmarks:

```
php test/benchmark.php

```

Results on PHP 8.1 (100,000 iterations per test):

### Match performance

[](#match-performance)

ScenarioSmall router (7 routes)Large router (500+ routes)Static route~695K ops/sec~386K ops/secParam with regex~324K ops/sec~242K ops/secTwo params, deep path~175K ops/sec~167K ops/secWildcard with key/value pairs~170K ops/sec~242K ops/secCatchall (`/*`)~466K ops/sec~599K ops/secNo match~313K ops/sec—### Generate performance

[](#generate-performance)

ScenarioSmall routerLarge routerStatic route~1.3M ops/sec—Single param~468K ops/sec~408K ops/secTwo params~516K ops/sec~244K ops/secWildcard with params~317K ops/sec~592K ops/sec### Build and memory

[](#build-and-memory)

MetricSmall (7 routes)Large (500+ routes)Build speed~21K ops/sec~327 ops/secMemory footprint—~744 KBThe tree structure scales well: matching with 500+ routes is only 30–50% slower than with 7 routes, since lookup depends on tree depth rather than total route count. At ~170K+ ops/sec in the worst case, routing takes roughly 6 microseconds per request.

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance56

Moderate activity, may be stable

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/729576?v=4)[Igor Crevar](/maintainers/igorcrevar)[@igorcrevar](https://github.com/igorcrevar)

---

Top Contributors

[![igorcrevar](https://avatars.githubusercontent.com/u/729576?v=4)](https://github.com/igorcrevar "igorcrevar (10 commits)")

### Embed Badge

![Health badge](/badges/igorcrevar-icrouter/health.svg)

```
[![Health](https://phpackages.com/badges/igorcrevar-icrouter/health.svg)](https://phpackages.com/packages/igorcrevar-icrouter)
```

###  Alternatives

[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k39.6M299](/packages/laravel-dusk)[nineinchnick/edatatables

Grid widget for the Yii Framework, wrapper for the DataTables jQuery plugin

173.2k](/packages/nineinchnick-edatatables)[link-cloud/fast-hyperf

LinkCloud Fast Hyperf

241.2k1](/packages/link-cloud-fast-hyperf)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
