thewunder/croute
Composer 安装命令:
composer require thewunder/croute
包简介
Convention based routing for PHP
README 文档
README
Convention based routing for PHP based on Symfony components.
Croute is great because:
- You don't need to maintain a routing table
- Promotes consistent code organization
- Allows for customization through attributes and events
Install via Composer
Via the command line:
composer.phar require thewunder/croute ^2.0
Or add the following to the require section your composer.json:
"thewunder/croute": "^2.0"
Basics
Your index.php should look something like this:
$router = Router::create($eventDispatcher, ['Your\\Controller\\Namespace'], $container, [$dependency1, $dependency2]); $router->route($request);
Your controllers should look something like this:
namespace Your\Controller\Namespace class IndexController extends Croute\Controller { public function __construct($dependency1, $dependency2) { //... } /** * Will be available at http://yourdomain/ * and require the "required" (body or querystring) request parameter */ public function indexAction($required, $optional = null) { echo 'Crouter Controller'; //you can echo or return a symfony Response } /** * Available at http://yourdomain/test */ public function testAction() { return new Response('Test Action'); } }
The name of the controller determines which url it appears as:
- http://yourdomain/my/ -> Your\Controller\Namespace\MyController::indexAction()
- http://yourdomain/my/action -> Your\Controller\Namespace\MyController::actionAction()
It supports nested namespaces so that:
- http://yourdomain/level1/level2/save -> Your\Controller\Namespace\Level1\Level2\IndexController::saveAction()
Attributes
Croute optionally supports controller and action attributes. Two attributes are included out of the box, HttpMethod and Secure.
HttpMethod
Restricts the allowed http methods. Returns a 400 response if the method does not match.
#[HttpMethod('POST')] public function saveAction()
Secure
Requires a secure connection. If the connection is not https send a 301 redirect to the same url with the https protocol.
#[Secure] class IndexController extends Controller {
Custom Attributes
To create a custom attribute, implement the RoutingAttribute interface on your attribute, and an AttributeHandler.
#[\Attribute(\Attribute::TARGET_CLASS|\Attribute::TARGET_METHOD)] class MyAttribute implements RoutingAttribute { public function __construct(public string $option) {} }
class MyAttributeHandler extends BasicAttributeHandler { public function getAttributeClass(): string { return MyAttribute::class; } public function handleRequest(MyAttribute|RoutingAttribute $attribute, Request $request): ?Response { // Return a response will immediately return that response, bypassing the normal controller action if ($attribute->option == 'teapot') { return new Response("I'm a teapot", Response::HTTP_I_AM_A_TEAPOT); } return null; } }
Add the attribute handler to the router, and your custom attribute will be ready to use.
$router->addAttributeHandler(new MyAttributeHandler());
Events
Symfony events are dispatched for every step in the routing process. A total of 12 events are dispatched in a successful request:
- router.request
- router.controller_loaded
- router.controller_loaded.{ControllerName}
- router.before_action
- router.before_action.{ControllerName}
- router.before_action.{ControllerName}.{actionName}
- router.after_action
- router.after_action.{ControllerName}
- router.after_action.{ControllerName}.{actionName}
- router.before_response_sent
- router.response_sent
- router.response_sent.{ControllerName}
- router.response_sent.{ControllerName}.{actionName}
The {ControllerName} will be sans 'Controller' and {actionName} sans 'Action' i.e IndexController::indexAction -> router.before_action.Index.index.
At any time before the response is sent, in an event listener you can set a response on the event to bypass the action and send instead.
public function myListener(ControllerLoadedEvent $event) { $event->setResponse(new Response('PermissionDenied', 403)); }
Error Handling
Proper error handling is not really something that I can do for you. It's up to you to determine how to do logging, how and when to render a pretty error page. To handle errors, implement the EventHandlerInterface and set your error handler on the router. Your class will be called when common routing events occur (i.e. 404 errors) and when there is an exception during the routing process.
Contributing
Please see CONTRIBUTING for details.
thewunder/croute 适用场景与选型建议
thewunder/croute 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.43k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2014 年 09 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「routing」 「router」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 thewunder/croute 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 thewunder/croute 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 thewunder/croute 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Write down your routing mapping at one place
Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.
Router
PMVC App Action Router
Database alias router extension for Nette Framework
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
统计信息
- 总下载量: 19.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 14
- 点击次数: 3
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-09-26