ecfectus / router
基于fast route的PHP 7路由实现
dev-master
2016-11-24 19:54 UTC
Requires
- php: >=7.0.0
- php-ds/php-ds: ^1.1
Requires (Dev)
- phpdocumentor/phpdocumentor: 2.*
- phpunit/phpunit: ^5.5.0
This package is not auto-updated.
Last update: 2024-09-14 19:31:22 UTC
README
PHP 7路由实现。
ecfectus路由器是一个无依赖项的轻量级、快速且可缓存的PHP路由器。
通过不需要了解任何请求,该路由器被设计得非常简洁,它简单地匹配路径模式与路由。
用法
$router = new \Ecfectus\Router\Router(); /** * Basic Requests */ //get requests $router->get('/url') ->setHandler('Class@method'); //post requests $router->post('/url') ->setHandler('Class@method'); //put requests $router->put('/url') ->setHandler('Class@method'); //patch requests $router->patch('/url') ->setHandler('Class@method'); //delete requests $router->delete('/url') ->setHandler('Class@method'); /** * Route Params */ //args and optional args $router->get('/url/{arg}/{optionalarg?}') ->setHandler('Class@method'); //only matches number $router->get('/url/{arg:number}') ->setHandler('Class@method'); //only matches words $router->get('/url/{arg:word}') ->setHandler('Class@method'); //only matches alpha numeric + dashs $router->get('/url/{arg:alphanumdash}') ->setHandler('Class@method'); //only matches slugs $router->get('/url/{arg:slug}') ->setHandler('Class@method'); //only matches uuids $router->get('/url/{arg:uuid}') ->setHandler('Class@method'); /** * Named Routes */ $router->get('/url') ->setName('routename') ->setHandler('Class@method'); /** * Domain Routes */ $router->get('/url') ->setDomain('domain.com') ->setHandler('Class@method'); // domain placeholders $router->get('/url') ->setDomain('{subdomain}.domain.com') ->setHandler('Class@method'); /** * Grouped routes */ $router->group([ 'path' => '/api', 'name' => 'api', 'domain' => 'domain.com' ], function($r){ $r->get('/users') // becomes domain.com/api/users ->setName('users') // becomes api.users ->setHandler('Class@method'); //groups can be nested }); /** * Prepare Router before matching */ try{ $router->prepare(); }catch( Exception $e){ } /** * Match routes */ try{ $route = $router->match('hello.domain.com/url', 'GET'); // $path, $method = 'GET|POST|PUT|PATCH|DELETE' $values = $route->getValues(); // ['subdomain' => 'hello'] }catch( \Ecfectus\Router\NotFoundException $e){ //no route matched }catch( \Ecfectus\Router\MethodNotAllowedException $e){ //route matched but method not allowed }catch( \Exception $e){ }
缓存路由器
使用缓存路由器,您可以导出编译后的路由器,并从文件中恢复实例,从而绕过编译过程并提高性能。
$router = \Ecfectus\Router\CachedRouter::create('routes.php'); if(!$router->isCached()){ //add routes here try{ //prepare and export the router to the file. $router->prepare(); $router->export(); }catch( Exception $e){ } } try{ $route = $router->match('hello.domain.com/url', 'GET'); // $path, $method = 'GET|POST|PUT|PATCH|DELETE' $values = $route->getValues(); // ['subdomain' => 'hello'] }catch( \Ecfectus\Router\NotFoundException $e){ //no route matched }catch( \Ecfectus\Router\MethodNotAllowedException $e){ //route matched but method not allowed }catch( \Exception $e){ }