racoon/router
Composer 安装命令:
composer require racoon/router
包简介
A basic extension of nikic/fast-route.
README 文档
README
Routing
Racoon uses nikic/fast-route to deal with routing.
Defining where routes are stored
Routes need to be added in a routes file, which should be added to the Router, or defined in an anonymous function passed to the router.
$router->addRouteFile('/path/to/some_routes.php'); // Tells the router you are done adding routes so as it can process them. $router->init();
If you want to store routes in multiple locations you can do it as follows.
$router ->addRouteFile('/path/to/some_routes.php') ->addRouteFile('/path/to/more_routes.php') ->addRouteFile('/path/to/even_more_routes.php'); // Tells the router you are done adding routes so as it can process them. $router->init();
If you define multiple route locations, they will be included/added in the same order as you define them.
To define routes without storing them in a separate file you can do the following.
$router->addRouteCallable(function($r) { // Define your routes here as if they were in another file. // $r->addRoute(), $r->addGroup(), etc are all available. });
Setting up routes
Inside one of the route files that have been added to the router you need to define your routes in the following format.
$httpRequestMethod = ['GET', 'POST']; $requestUri = '/users/list'; $handlerString = '\\MyApp\\Users@list'; $r->addRoute($httpRequestMethod, $requestUri, $handlerString);
Route Groups
To make it easier to set up long urls multiple times you can use groups. The following code blocks give the same result.
$r->addRoute(['GET', 'POST'], '/users/list', '\\MyApp\\Users@list'); $r->addRoute(['GET', 'POST'], '/users/get', '\\MyApp\\Users@get'); $r->addRoute(['GET', 'POST'], '/users/update', '\\MyApp\\Users@update'); $r->addRoute(['GET', 'POST'], '/users/delete', '\\MyApp\\Users@delete');
$r->addGroup('/users', function () { $r->addRoute(['GET', 'POST'], '/list', '\\MyApp\\Users@list'); $r->addRoute(['GET', 'POST'], '/get', '\\MyApp\\Users@get'); $r->addRoute(['GET', 'POST'], '/update', '\\MyApp\\Users@update'); $r->addRoute(['GET', 'POST'], '/delete', '\\MyApp\\Users@delete'); });
You can also use sub-groups. The following code blocks give the same result.
$r->addRoute(['GET', 'POST'], '/some/long/url/do-something', '\\MyApp\\Users@list');
$r->addGroup('/some', function ($r) { $r->addGroup('/long', function ($r) { $r->addGroup('/url', function ($r) { $r->addRoute(['GET', 'POST'], '/do-something', '\\MyApp\\Users@list'); }); }); });
HTTP Request Method
The HTTP Request Method(s) that the route should match. This can be any HTTP request type such as GET or POST.
Can be a string or an array of strings.
Request URI
The request URI that the route should match.
You can define the request URI in multiple ways.
'/users/list' '/users/get/{userId}' '/users/get/{userId:\d+}'
For more information see the FastRoute Route Docs
Any wildcards/placeholders defined here will be passed into the Controller/Handler method.
Handler String
The handler string defines the class and method that should be executed should the current request match a route.
The required format is \MyApp\Users@list where \MyApp\Users is the full class name including the namespace, and list is the method inside of that class which you want to be executed.
racoon/router 适用场景与选型建议
racoon/router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 476 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 06 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 racoon/router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 racoon/router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 476
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2016-06-13