carlosartur/slim-auto-routing-controller
Composer 安装命令:
composer require carlosartur/slim-auto-routing-controller
包简介
Auto generate routes from class methods on slim framework.
README 文档
README
This library adds a abstract controller to auto generate routes for a slim framework project.
How to use
Install
composer require carlosartur/slim-auto-routing-controller
Supported HTTP Methods
- GET
- POST
- PUT
- DELETE
- OPTIONS
- PATCH
Usage example
How it works
- To generate path
- If the controller has "ROUTE_FIXED" constant defined:
- Use value of "ROUTE_FIXED" constant as path.
- Else:
- Get the controller's name.
- Remove "Controller" word of it.
- Slugify the remaining string.
- Prefix it with value of "ROUTE_PREFIX" constant (optional).
- If the controller has "ROUTE_FIXED" constant defined:
- To generate a route:
- Searches on controller the public methods which have the word "Action" on it's name, in any part of name.
- To change the "Action" word to anything else, add a "METHOD_ROUTE_SUFFIX" constant on controller, with another word.
- To generate a "GET" action, the method must have the "GET" word inside it's name.
- To generate a "POST" action, the method must have the "POST" word inside it's name.
- To generate a "PUT" action, the method must have the "PUT" word inside it's name.
- To generate a "DELETE" action, the method must have the "DELETE" word inside it's name.
- To generate a "OPTIONS" action, the method must have the "OPTIONS" word inside it's name.
- To generate a "PATCH" action, the method must have the "PATCH" word inside it's name.
- If method's name is more than <verb><suffix>:
- Remove http verb and suffix from name;
- Slugify the remaining string
- Add result of slug to path
- Searches on controller the public methods which have the word "Action" on it's name, in any part of name.
- Parameters and Validation parameter's regex:
- To add parameter, just add it on method, after "Request" and "Response" parameters.
- To validate it, 2 ways are possible
- By type:
- Add a "TYPES_REGEX" constant on your controller, or use the library default, wich validates float and int only.
- By name:
- Add a "PARAMETER_VALIDATION_REGEX" constant on your controller, or use the library default, wich validates a parameter named "id" as a integer value.
- By type:
Controller
<?php use AutoRotingController\AutoRotingController; use Slim\Psr7\Request; use Slim\Psr7\Response; class ExampleController extends AutoRotingController { /** Method to generate a "GET" /example route */ public function getAction(Request $request, Response $response) {} /** Method to generate a "POST" /example route */ public function postAction(Request $request, Response $response) {} /** Method to generate a "PUT" /example route */ public function putAction(Request $request, Response $response) {} /** Method to generate a "DELETE" /example route */ public function deleteAction(Request $request, Response $response) {} /** Method to generate a "OPTIONS" /example route */ public function optionsAction(Request $request, Response $response) {} /** Method to generate a "PATCH" /example route */ public function patchAction(Request $request, Response $response) {} /** Method to generate a "GET" /example/foo-bar route */ public function getFooBarAction(Request $request, Response $response) {} /** Method to generate a "POST" /example/foo-bar route */ public function postFooBarAction(Request $request, Response $response) {} /** Method to generate a "PUT" /example/foo-bar route */ public function putFooBarAction(Request $request, Response $response) {} /** Method to generate a "DELETE" /example/foo-bar route */ public function deleteFooBarAction(Request $request, Response $response) {} /** Method to generate a "OPTIONS" /example/foo-bar route */ public function optionsFooBarAction(Request $request, Response $response) {} /** Method to generate a "PATCH" /example/foo-bar route */ public function patchFooBarAction(Request $request, Response $response) {} }
On your routing file
Instead of doing this
<?php $app->get('/example', ExampleController::class . ':getAction'); $app->post('/example', ExampleController::class . ':postAction'); $app->put('/example', ExampleController::class . ':putAction'); $app->delete('/example', ExampleController::class . ':deleteAction'); $app->options('/example', ExampleController::class . ':optionsAction'); $app->patch('/example', ExampleController::class . ':patchAction');
It's necessary only run this
ExampleController::generateRoutes($app);
Configuration
To configure each controller individually, some constants can be used:
/** Path prefix, to add a prefix on route */ protected const ROUTE_PREFIX = ""; /** To use a fixed path for all routes of a controller */ protected const ROUTE_FIXED = ""; /** The route methods has to have this value. All other methods of the class will be ignored */ protected const METHOD_ROUTE_SUFFIX = 'Action'; /** The prefix of the method used to call route function. You can implement your own callers. */ protected const CALL_FUNCTION_PREFIX = "callRouteMethod"; /** The argument regexes. The key is it's type. */ public const TYPES_REGEX = [ "int" => '[0-9]+', "float" => '[+-]?([0-9]*[.])?[0-9]+', ]; /** The argument regexes. The key is it's name. */ public const PARAMETER_VALIDATION_REGEX = [ "id" => '[0-9]+', ];
carlosartur/slim-auto-routing-controller 适用场景与选型建议
carlosartur/slim-auto-routing-controller 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 10 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「controller」 「router」 「slimframework」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 carlosartur/slim-auto-routing-controller 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 carlosartur/slim-auto-routing-controller 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 carlosartur/slim-auto-routing-controller 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Give your JS App some Backbone with Models, Views, Collections, and Events.
Class generating JSON output form mysql/pdo pgsql/pdo usable for Javascript library DataTables
Router
PMVC App Action Router
Slim 3 found handler
Database alias router extension for Nette Framework
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-10-16