opsway / slim-attribute-router
Slim attribute router with support of groups and middlewares
Installs: 2 286
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- beberlei/assert: ^3.3.0
- slim/slim: 4.*
Requires (Dev)
- opsway/psr12-strict-coding-standard: ^0.5.0
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.22
README
Slim attribute router
This package allows you to add routes to your slim4 (https://www.slimframework.com) application using attributes.
Features
- Route methods support
- Route name support
- Route group support
- Middlewares support
Installation
This package can be installed using Composer
Navigate into your project's root directory and execute the bash command shown below
composer require opsway/slim-attribute-router
Enabling the Attribute Router
Attribute router extends slims default RouteCollector, all you need to do is instantiate attribute router and pass it on to AppFactory. First parameter is array of namespace parts of app. It will be used for filtering classes in which search of parameters will go on.
<?php use OpsWay\Slim\AttributeRouter\Router; use Slim\Factory\AppFactory; $route = new Router([ ['NameSpace'], // array of namespaces parts of app AppFactory::determineResponseFactory(), new CallableResolver($container) // optional DI container ); AppFactory::setRouteCollector($attributeRouteCollector); $app = AppFactory::create(); $app->run();
Attribute signature
#[Route({methods}, {path}[[, {group}][, {name}][, {isDeprecated}]])]
- The "methods" parameter is required and must be not empty
- The "path" parameter is required and must be not empty
- Rest of parameters are optional
#[Group({name} [[, {classes}]])]
- The "name" parameter is required and must be compatible with URI string requirements
- The "classes" parameter is optional. List of classes implementing Psr\Http\Server\MiddlewareInterface
#[Middlewares({firstClass} [, {secondClass}])]
- The "firstClass" parameter is required and must class name of class that implements Psr\Http\Server\MiddlewareInterface
- The "secondClass" and all other parameters is optional.
- All arguments of constructor are converted to an array of middleware classes. It should be at least one middleware class
Specifying attributes examples
Example specifying route with name and group parameters at class level
<?php use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; #[Route([Route::METHOD_GET], '/api/hello-world', '/hello-world-group', 'api.hello-world.route-name')] class HelloWorld { public function __invoke(Request $request, Response $response): Response { // some php code return $response; } }
Example specifying route without additional parameters at method level
<?php use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; class HelloWorld { #[Route([Route::METHOD_GET], '/api/hello-world')] public function __invoke(Request $request, Response $response): Response { // some php code return $response; } }
Example specifying group with middlewares at class level
<?php use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; #[Group('/api', [FirstMidleware::class, SecondMidleware::class])] class HelloWorld { public function __invoke(Request $request, Response $response): Response { // some php code return $response; } }