PHP Classes

icRouter: Route accesses based on request parameters

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2026-06-09 (6 days ago) RSS 2.0 feedStarStarStar 49%Total: 698 All time: 4,698 This week: 63Up
Version License PHP version Categories
icrouter 0.91MIT/X Consortium ...5.3HTTP, PHP 5, Design Patterns
Description 

Author

This package can route accesses based on request parameters.

It can get the HTTP request parameters and determine which controller action should be dispatched based on routes that can be defined by URL patterns similar to Symfony framework routing subsystem.

The package can return the request parameters from a matched rule.

Picture of Igor Crevar
Name: Igor Crevar <contact>
Classes: 2 packages by
Country: Serbia Serbia
Age: 44
All time rank: 261310 in Serbia Serbia
Week rank: 195 Up1 in Serbia Serbia Up

Details

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

composer require pathforge/icrouter

Usage

Setup

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

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

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

$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

$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

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

| Argument | Description | |---|---| | $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

  • 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

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

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

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

$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

vendor/bin/phpunit test/RouterTest.php

Benchmarks

Run benchmarks:

php test/benchmark.php

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

Match performance

| Scenario | Small router (7 routes) | Large router (500+ routes) | |---|---|---| | Static route | ~695K ops/sec | ~386K ops/sec | | Param with regex | ~324K ops/sec | ~242K ops/sec | | Two params, deep path | ~175K ops/sec | ~167K ops/sec | | Wildcard with key/value pairs | ~170K ops/sec | ~242K ops/sec | | Catchall (/*) | ~466K ops/sec | ~599K ops/sec | | No match | ~313K ops/sec | — |

Generate performance

| Scenario | Small router | Large router | |---|---|---| | Static route | ~1.3M ops/sec | — | | Single param | ~468K ops/sec | ~408K ops/sec | | Two params | ~516K ops/sec | ~244K ops/sec | | Wildcard with params | ~317K ops/sec | ~592K ops/sec |

Build and memory

| Metric | Small (7 routes) | Large (500+ routes) | |---|---|---| | Build speed | ~21K ops/sec | ~327 ops/sec | | Memory footprint | — | ~744 KB |

The 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.


  Files folder image Files (18)  
File Role Description
Files folder imagesrc (1 directory)
Files folder imagetest (2 files)
Accessible without login Plain text file composer.json Data composer.json
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. LICENSE
Accessible without login Plain text file README.md Data readme

  Files folder image Files (18)  /  src  
File Role Description
Files folder imagePathForge (1 directory)

  Files folder image Files (18)  /  src  /  PathForge  
File Role Description
Files folder imageicRouter (3 files, 2 directories)

  Files folder image Files (18)  /  src  /  PathForge  /  icRouter  
File Role Description
Files folder imageInterfaces (1 file, 1 directory)
Files folder imageNodes (7 files)
  Plain text file Route.php Class Class source
  Plain text file Router.php Class Class source
  Plain text file RouterException.php Class Class source

  Files folder image Files (18)  /  src  /  PathForge  /  icRouter  /  Interfaces  
File Role Description
Files folder imageDefImpl (1 file)
  Plain text file INodeBuilder.php Class Class source

  Files folder image Files (18)  /  src  /  PathForge  /  icRouter  /  Interfaces  /  DefImpl  
File Role Description
  Plain text file DefaultNodeBuilder.php Class Class source

  Files folder image Files (18)  /  src  /  PathForge  /  icRouter  /  Nodes  
File Role Description
  Plain text file ARouteNode.php Class Class source
  Plain text file RouteNode.php Class Class source
  Plain text file RouteNodeLeaf.php Class Class source
  Plain text file RouteNodeLeafAny.php Class Class source
  Plain text file RouteNodeParam.php Class Class source
  Plain text file RouteNodeParamSimple.php Class Class source
  Plain text file RouteNodeRoot.php Class Class source

  Files folder image Files (18)  /  test  
File Role Description
  Accessible without login Plain text file benchmark.php Example Example script
  Accessible without login Plain text file RouterTest.php Test phpunit tests

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:698
This week:0
All time:4,698
This week:63Up
User Ratings User Comments (1)
 All time
Utility:70%StarStarStarStar
Consistency:75%StarStarStarStar
Documentation:-
Examples:-
Tests:65%StarStarStarStar
Videos:-
Overall:49%StarStarStar
Rank:2913