PHPackages                             pathforge/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. [HTTP &amp; Networking](/categories/http)
4. /
5. pathforge/icrouter

ActiveLibrary[HTTP &amp; Networking](/categories/http)

pathforge/icrouter
==================

Tree-based URL router for PHP. Efficient matching via node tree instead of regex iteration.

00PHP

Since Apr 6Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/igorcrevar/icRouter)[ Packagist](https://packagist.org/packages/pathforge/icrouter)[ RSS](/packages/pathforge-icrouter/feed)WikiDiscussions master Synced 3w 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

18

—

LowBetter than 7% of packages

Maintenance53

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity12

Early-stage or recently created project

 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/pathforge-icrouter/health.svg)

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

###  Alternatives

[php-http/cache-plugin

PSR-6 Cache plugin for HTTPlug

25026.1M82](/packages/php-http-cache-plugin)[httpsoft/http-message

Strict and fast implementation of PSR-7 and PSR-17

87965.9k122](/packages/httpsoft-http-message)[serpapi/google-search-results-php

Get Google, Bing, Baidu, Ebay, Yahoo, Yandex, Home depot, Naver, Apple, Duckduckgo, Youtube search results via SerpApi.com

69127.2k](/packages/serpapi-google-search-results-php)[swoft/websocket-server

swoft websocket server component

16135.7k5](/packages/swoft-websocket-server)[thesis/nats

Async (fiber based) client for Nats.

754.4k](/packages/thesis-nats)[jasny/http-signature

Implementation of the IETF HTTP Signatures draft RFC

10104.7k](/packages/jasny-http-signature)

PHPackages © 2026

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