eserozvataf / scabbia2-router
此包已废弃,不再维护。未建议替代包。
Scabbia2 路由组件
v0.1.2
2015-09-16 07:03 UTC
Requires
- php: >=5.6.0
Requires (Dev)
This package is auto-updated.
Last update: 2023-09-08 20:01:57 UTC
README
此组件是一个简单的路由分发器,它解析并将路由分发到回调或控制器。
使用方法
创建路由定义
use Scabbia\Router\RouteCollection; $routes = new RouteCollection(); // adding a static route $routes->addRoute('GET', '/about', 'AboutController::IndexAction'); // adding a static route with multiple http methods $routes->addRoute(['GET', 'POST'], '/about', 'AboutController::IndexAction'); // adding a dynamic route $routes->addRoute('GET', '/users/profile/{id:[a-z]+}', 'UsersController::ProfileAction'); // adding a dynamic route with a routing name $routes->addRoute('GET', '/users/posts/{id:[a-z]+}', 'UsersController::PostsAction', 'user/posts');
保存路由定义
file_put_contents('routes.json', json_encode($routes->save()));
重新加载路由定义
$routes = json_decode(file_get_contents('routes.json'));
分发路由
use Scabbia\Router\Router; $router = new Router($routes); // initialize a new router with route definitions $route = $router->dispatch('GET', '/about'); if ($route['status'] === Router::FOUND) { call_user_func($route['callback'], ...$route['parameters']); }
使用名称进行反向路由
use Scabbia\Router\Router; $router = new Router($routes); // initialize a new router with route definitions echo $router->path('users/posts', [ 'id' => 'eser' ]);
链接
贡献
对任何贡献都公开。欢迎修复错误、新功能和额外模块。所有贡献应提交到eserozvataf/scabbia2-router存储库。