dev-master
9999999-devFast request router for PHP
MIT
The Requires
- php >=5.4.0
- ext-mbstring *
The Development Requires
by Ivan Vankov (gatakka)
router pgf
Wallogit.com
2017 © Pedro Peláez
Fast request router for PHP
This library does not use regular expressions, instead routes are converted in tree-like structure (it is a native multi dimensional array). So later searches are very effective because complexity is related to the count of request URL segments not to number of added routes. Building the tree is an expensive task but structure can be cached very effectively. See simple benchmark below., (*1)
This code is just a prove of concept., (*2)
Basic usage:, (*3)
include './vendor/autoload.php';
$router = new \PGF\Router\Router();
$router->addRoute('get', '/', 'HomeController@index');
$router->addRoute('get', '/user/', 'UserController@index');
$router->addRoute('get', '/user/{id}', 'UserController@show');
$router->addRoute('post', '/user/{id}', 'UserController@save');
//{user?} marks optional parameter
$router->addRoute('get', '/message/send/{user?}', 'MessagesController@send');
$router->findRoute('get', '/message/send/John')
Routes are added using:, (*4)
addRoute($method, $route, $action)
Available methods are: [get,post,put,delete, any]. A route can have more than one method:, (*5)
$router->addRoute(['get','post'], '/user/', 'UserController@index');
If a route is found an array is returned. Structure is:, (*6)
$router->findRoute('get', '/user/1/');
Array
(
[route] => /user/{id} // registered route
[method] => get // route method
[action] => UserController@show // action for this route
[params] => Array // route parameters with there names and values. Order is same as they are found in route.
(
[id] => 1
)
)
Because internal structure is plain multidimensional array, caching is very easy and effective. Data for caching can be obtain using:, (*7)
$router->dump();
And can be loaded using:, (*8)
$router->load($cachedArray);
Using load() method will overwrite any previously added routes. If you load data adding routes using addRoute() is not necessary, (*9)
All exceptions are in PGF\Router\Exceptions namespace., (*10)
InvalidMethodException is thrown when route is registered with invalid method., (*11)
MethodNotAllowedException is thrown when route is found but requested method is not allowed., (*12)
RouteNotFoundException is thrown when route is not found., (*13)
Using optional parameters can cause some issues. For example:, (*14)
$router->addRoute('get', '/', 'HomeController@index');
$router->addRoute('get', '/{id}', 'HomeController@show');
$router->addRoute('get', '/{id?}', 'HomeController@get');
HomeController@get will never match because if request URL is / then HomeController@index will be returned. If request URL is /123 then HomeController@show will be returned., (*15)
Tests are executed using Apache Benchmark and show requests/second:, (*16)
ab -n 10000 -c 50
| PGF/Router | nikic/fast-route --- | --- | --- 1 route (not cached - found) | 16400 | 13300 1 route (not cached - not found) | 14400 | 13700 30 routes (not cached - found) | 9400 | 6800 30 routes (not cached - not found) | 9200 | 6600 100 routes (not cached - found) | 4900 | 2800 100 routes (not cached - not found) | 4800 | 2700 100 routes (cached - found) | 10800 | 12000 100 routes (cached - not found) | 9900 | 9100 As you can see from results this implementation is very close to one of the fastest routing library., (*17)
Fast request router for PHP
MIT
router pgf