tarikweiss / slim-attribute-router
A simple library for applying routing with attributes.
0.1.1
2024-09-09 20:05 UTC
Requires
- php: ^8.1
- slim/slim: ^4
Requires (Dev)
- phpstan/phpstan: ^1.12
This package is auto-updated.
Last update: 2025-04-09 21:20:45 UTC
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, }; } }