sasa-b / router
轻量级且性能卓越的路由器,灵感来源于Laravel和Phalcon的路由器。
1.0.2
2017-10-09 18:55 UTC
Requires
- psr/container: ^1.0
This package is auto-updated.
Last update: 2024-09-14 10:50:21 UTC
README
受Laravel和Phalcon路由器启发的轻量级路由器。
使用示例
$router = new \Foundation\Routing\Router(); $r->get('/foo', function () { echo 'Hello foo!'; }); // here we are utilising cache for performance, if the cache file was not found // routes will be registered and the cache file recreated $router->cache(function (\Foundation\Routing\Router $r) { // if you want to collect routes from a file // you can set the path to the routes file as a paramater to collectRoutes() method // or via setRoutesPath() method $r->collectRoutes(); // you can both collect routes and add them one by one, they will be merged $r->get('/foo/{bar}', [ 'controller' => 'FooController', 'action' => 'index', ]); $r->post('/foo/{bar}', 'FooController::store'); }); try { // Adding event listeners $router->addEventListener('before_match', function(\Foundation\Routing\Router $router) { echo "before match"; }); $router->addEventListener('after_match', function(\Foundation\Routing\Router $router) { echo "after match"; }); $dispatcher = $router->catch(); $dispatcher->dispatch(); } catch (\Foundation\Routing\Exceptions\NotFoundException $e) { echo $e->getCode() . " - Page not found"; } catch (\Foundation\Routing\Exceptions\BadHttpMethodException $e) { echo $e->getCode() . " - Bad Http Method"; }