carlosartur / slim-auto-routing-controller
从Slim框架中的类方法自动生成路由。
This package is auto-updated.
Last update: 2024-09-17 17:26:02 UTC
README
此库为Slim框架项目添加了一个抽象控制器,用于自动生成路由。
如何使用
安装
composer require carlosartur/slim-auto-routing-controller
支持的HTTP方法
- GET
- POST
- PUT
- DELETE
- OPTIONS
- PATCH
使用示例
工作原理
- 生成路径
- 如果控制器定义了"ROUTE_FIXED"常量
- 使用"ROUTE_FIXED"常量的值作为路径。
- 否则
- 获取控制器的名称。
- 移除其中的"Controller"单词。
- 将剩余的字符串进行slugify。
- 使用"ROUTE_PREFIX"常量的值进行前缀(可选)。
- 如果控制器定义了"ROUTE_FIXED"常量
- 生成路由
- 在控制器上搜索具有其名称中任何部分包含"Action"单词的公共方法。
- 要将"Action"单词更改为其他内容,在控制器中添加一个包含另一个单词的"METHOD_ROUTE_SUFFIX"常量。
- 要生成"GET"动作,方法名称必须包含"GET"单词。
- 要生成"POST"动作,方法名称必须包含"POST"单词。
- 要生成"PUT"动作,方法名称必须包含"PUT"单词。
- 要生成"DELETE"动作,方法名称必须包含"DELETE"单词。
- 要生成"OPTIONS"动作,方法名称必须包含"OPTIONS"单词。
- 要生成"PATCH"动作,方法名称必须包含"PATCH"单词。
- 如果方法名称超过<verb><suffix>
- 从名称中移除HTTP动词和后缀;
- 对剩余的字符串进行slugify
- 将slug的结果添加到路径
- 在控制器上搜索具有其名称中任何部分包含"Action"单词的公共方法。
- 参数和验证参数的regex
- 要添加参数,只需将其添加到方法中,在"Request"和"Response"参数之后。
- 验证有两种方式
- 按类型
- 在你的控制器中添加一个"TYPES_REGEX"常量,或使用库默认值,它只验证float和int。
- 按名称
- 在你的控制器中添加一个"PARAMETER_VALIDATION_REGEX"常量,或使用库默认值,它将名为"id"的参数验证为整数。
- 按类型
控制器
<?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) {} }
在路由文件中
这样做是必要的
<?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');
只需要运行这个
ExampleController::generateRoutes($app);
配置
要单独配置每个控制器,可以使用一些常量
/** 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]+', ];