arminvieweg/slim-routing-manager
Composer 安装命令:
composer require arminvieweg/slim-routing-manager
包简介
Controller and annotation based routing for the Slim Framework
README 文档
README
This is a fork of https://github.com/LeaseWeb/slim-routing-manager
This repository was forked from JLaso's RoutingCacheManager, it features some enhancements such as a more advanced form of caching routes and support for the latest version of Slim.
I would like to thank Patrick JL Laso for providing the original code that this component is based on.
Initialising the Routing Manager
To initialise the routing manager, you need to add the following lines to your index.php file that bootstrap's your slim application:
// Initialize the Routing Manager
$routingManager = new RoutingManager(
array(
'/path/to/controllers/'
), '/path/to/cache/dir/'
);
$routingManager->generateRoutes();
The routing manager supports multiple directory's to scan for controllers. Which can be useful if you have a frontend/backend type system.
Route prefixing
The routing manager supports adding a prefix to each route (such as API version). This can be set by either running $routingManager->setRoutePrefix('/v1'); or by defining it in the constructor of the Routing Manager itself.
If there is a controller action you do not wish routes being prefixed for you can use the @noPrefix annotation.
Controller structure
The structure of the controller itself does not really matter for the Routing Manager. The only thing that it requires is defining the routes through annotations.
The following annotations are supported by the routing manager:
| Annotation | Value | Description |
|---|---|---|
| @Route | The slim-compliant route | Proving the route to the controller action |
| @noPrefix | None | Skip the prefix being attached to the route if a route prefix has been set |
| @Method | get/post/delete/put/option | The HTTP method used for the route |
| @Name | String | The name of the route |
The parameters and everything else related to the function itself does not defer from the official Slim standard. So default variables can be defined the same way you would normally define routes in Slim.
However we do recommend naming the controller actions xAction (similar to symfony 2 controller actions).
Example:
/**
* Class TestController
*
* @package Lsw\Controller
*/
class TestController
{
/**
* Test action
*
* @Route('/test/index(/:param1)(/:param2)')
*
* @Method('GET')
* @Name('test.index')
*
* @param $param1
* @param $param2
*
*/
public function indexAction($param1 = 1, $param2 = 2)
{
echo sprintf('param1: %s, param2: %s', $param1, $param2);
}
}
Multiple routes
The Routing manager also supports multiple routes to be defined for each action:
/**
* Test action
*
* @Route('/test/index(/:param1)(/:param2)')
* @Route('/alternative-route/index(/:param1)(/:param2)')
* @Route('/alternative-route-2/index/:param1/:param2')
*
* @Method('GET')
* @Name('test.index')
*
* @param $param1
* @param $param2
*
*/
The following routes will be generated:
$app->map("/test/index(/:param1)(/:param2)", "\Lsw\Controller\TestController:indexAction")->via("GET")->name("test.index");
$app->map("/alternative-route/index(/:param1)(/:param2)", "\Lsw\Controller\TestController:indexAction")->via("GET")->name("test.index");
$app->map("/alternative-route2/index/:param1/:param2", "\Lsw\Controller\TestController:indexAction")->via("GET")->name("test.index");
All the routes are processed independently from each other. This means some routes can require parameters to be passed and some can leave them optional in the route. The actual requirement check which throws an exception however is controlled by the controller action and not by the route itself so it only impacts the validity of the route.
Caching
The 'cache' path must be writable for the http/apache daemon user.
The very nature of this library is to create slim routes based on annotations. So the routing manager will scan the controllers only if the controllers themselves are changed or the cached routes file is being removed.
By default the generated compiled_routes.php will generate the following structure:
/**
* Generated with \Lsw\Routing\Manager
*
* on 2015-02-10 02:24:41
*/
$modTimes = array (
'/srv/shared/sho/RoutingManagerTest/src/Lsw/Controller/TestController.php' => 1423574678
);
$app = Slim\Slim::getInstance();
$app->map("/test/index", "\Lsw\Controller\TestController:indexAction")->via("GET")->name("test.index");
The modTimes array stores the modification times of the controllers that are being scanned by the routing manager. If a controller is changed the file's modification time will differ from the one stored in this array and the cache will be refreshed.
arminvieweg/slim-routing-manager 适用场景与选型建议
arminvieweg/slim-routing-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 10 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「routing」 「router」 「slim」 「slimframework」 「routing manager」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 arminvieweg/slim-routing-manager 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 arminvieweg/slim-routing-manager 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 arminvieweg/slim-routing-manager 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Secure session middleware for Slim 3 framework
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.
Adds request-parameter validation to the SLIM 3.x PHP framework
Router
Slim Framework 3 CSRF protection middleware utilities
统计信息
- 总下载量: 40
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-05