tarikweiss/slim-attribute-router
最新稳定版本:0.1.1
Composer 安装命令:
composer require tarikweiss/slim-attribute-router
包简介
A simple library for applying routing with attributes.
README 文档
README
This is a small library for mapping routes with an attribute.
Requirements
- PHP 8.1 or higher
- Slim 4.0 or higher
Installation
composer require tarikweiss/slim-attribute-router
Installation of router
$router = new \Tarikweiss\SlimAttributeRouter\Router( ['/path/to/project/src/Action'], new \Tarikweiss\SlimAttributeRouter\PublicMethodRouteTargetCreator('run') ); /** @var \Slim\App $slimApp */ $router->registerRoutes($slimApp);
Installation of Route attribute
The GET and POST is installed on class level, the PATCH method is installed on method level.
#[\Tarikweiss\SlimAttributeRouter\Route( methods: ['GET', 'POST'], path: '/foo' )] class FooAction { /** * Here the name given in the constructor of the PublicMethodRouteTargetCreator is used. */ public function run( \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $arguments ): \Psr\Http\Message\ResponseInterface { // Do you request handling and respond to the request. // This structure is given by Slim! // Have a look at the docs: https://www.slimframework.com/docs/v4/start/installation.html#step-4-hello-world return $response; } #[\Tarikweiss\SlimAttributeRouter\Route( methods: ['PATCH'], path: '/foo' )] public function anotherActionHandler( \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $arguments ): \Psr\Http\Message\ResponseInterface { // Same structure as above, but different scope. return $response; } } }
Custom RouteTargetCreator
If you require to create a custom callable or callable string for Slim's route registration, then you may implement the \Tarikweiss\SlimAttributeRouter\RouteTargetCreator interface.
It gives you full handling over the registration level (class or method) and the class (and method) that should be registered.
Here is a sample implementation, the RouteTargetCreator for public methods (shipped with library).
class PublicMethodRouteTargetCreator implements \Tarikweiss\SlimAttributeRouter\RouteTargetCreator { public function __construct( public string $classLevelMethodName = 'run' ) { } public function createRouteTarget(\Tarikweiss\SlimAttributeRouter\RouteLevel $routeLevel, string $class, ?string $method): callable|string { return match ($routeLevel) { \Tarikweiss\SlimAttributeRouter\RouteLevel::LEVEL_CLASS => $class . ':' . $this->classLevelMethodName, \Tarikweiss\SlimAttributeRouter\RouteLevel::LEVEL_METHOD => $class . ':' . $method, }; } }
统计信息
- 总下载量: 120
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-08-29