This is a small library for mapping routes with an attribute.
- PHP 8.1 or higher
- Slim 4.0 or higher
composer require tarikweiss/slim-attribute-router
$router = new \Tarikweiss\SlimAttributeRouter\Router(
['/path/to/project/src/Action'],
new \Tarikweiss\SlimAttributeRouter\PublicMethodRouteTargetCreator('run')
);
/** @var \Slim\App $slimApp */
$router->registerRoutes($slimApp);
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;
}
}
}
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,
};
}
}