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
2024-01-09 (9 months ago) RSS 2.0 feedStarStarStar 49%Total: 697 All time: 4,705 This week: 59Up
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: 42
All time rank: 261210 in Serbia Serbia
Week rank: 192 Up2 in Serbia Serbia Up

Details

icRouter

Different approach for standard routing problem. Instead of using regular expressions, matching tree is built.

This library will not work with older versions of PHP. Minimal version is 5.3

Usage

Usings

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

Create router

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

Add some 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+')), // a is integer
    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]+')), // b is string / number of 0' and 1'
    new Route('complex_param', '/complex/id_:id',
              array('module' => 'complex_param'),
              array('id' => '\d+')),
    new Route('home', '/*', 
              array('module' => 'home')),
]);

Build route tree

$router->build();

Match

$result = $router->match('/a/b/c/d/e');

$result will be array of matching parameters (key value pairs) if route exists otherwise false is returned

Generate

$result = $router->generate('two_params', array('b' => 'aabb'));

First parameter is route name, second is parameters (key value pairs) array

Route constructor parameters

  • name of route
  • patterns: Examples: 1. /acount/:id/* - provides functionality for additional parameters 2. /account/:id/:action 3. /account/id_:id Parameter is specified with /:[A-Za-z0-9]+/ Only one parameter is allowed per route segment
  • optional default parameters for route (key - value pairs)
  • optional regex for parameters in pattern Examples: 1. array('id' => '\d+') - id is integer 2. array('type' => 'car|boat|plane') - type is either car or boat or plane

Tip

For production, because $router->build() is expensive you should cache already built router (APC, serializing, etc...)

Unit Testing

Go to base dir and execute:

phpunit test/RouterTest.php

TODO

perfomance banchmark beetween this library and some regular expression routing library like one from symfony framework or similar.


  Files folder image Files (16)  
File Role Description
Files folder imagesrc (1 directory)
Files folder imagetest (1 file)
Accessible without login Plain text file composer.json Data composer.json
Accessible without login Plain text file LICENSE Lic. LICENSE
Accessible without login Plain text file README.md Data readme

  Files folder image Files (16)  /  src  
File Role Description
Files folder imageIgorCrevar (1 directory)

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

  Files folder image Files (16)  /  src  /  IgorCrevar  /  icRouter  
File Role Description
Files folder imageInterfaces (1 file, 1 directory)
Files folder imageNodes (7 files)
  Plain text file Route.php Class Defines Route
  Plain text file Router.php Class Main class
  Plain text file RouterException.php Class Exception class

  Files folder image Files (16)  /  src  /  IgorCrevar  /  icRouter  /  Interfaces  
File Role Description
Files folder imageDefImpl (1 file)
  Plain text file INodeBuilder.php Class Interface for node builder

  Files folder image Files (16)  /  src  /  IgorCrevar  /  icRouter  /  Interfaces  /  DefImpl  
File Role Description
  Plain text file DefaultNodeBuilder.php Class Default implementation od node builder

  Files folder image Files (16)  /  src  /  IgorCrevar  /  icRouter  /  Nodes  
File Role Description
  Plain text file ARouteNode.php Class Abstract tree node class
  Plain text file RouteNode.php Class Static route node
  Plain text file RouteNodeLeaf.php Class Leaf node
  Plain text file RouteNodeLeafAny.php Class * lead node
  Plain text file RouteNodeParam.php Class Node with pattern param
  Plain text file RouteNodeParamSimple.php Class Node with simple parameter
  Plain text file RouteNodeRoot.php Class Root node

  Files folder image Files (16)  /  test  
File Role Description
  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:697
This week:0
All time:4,705
This week:59Up
User Ratings User Comments (1)
 All time
Utility:70%StarStarStarStar
Consistency:75%StarStarStarStar
Documentation:-
Examples:-
Tests:65%StarStarStarStar
Videos:-
Overall:49%StarStarStar
Rank:2903