rodrifarias / slim-route-attributes
Slim Route Attributes is a route scanner that uses PHP attributes
0.4.0
2022-09-28 01:42 UTC
Requires
- php: >=8.1
- slim/psr7: ^1.5
- slim/slim: ^4.9
- symfony/cache: ^6.0
- symfony/console: ^6.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.6
This package is auto-updated.
Last update: 2024-04-15 20:13:39 UTC
README
Slim Route Attributes is a route scanner that uses PHP attributes.
Installation
$ composer require rodrifarias/slim-route-attributes
Hello World using AppFactory
Create file public/index.php.
<?php require_once __DIR__ . '/vendor/autoload.php'; use Rodrifarias\SlimRouteAttributes\App\AppSlimFactory; use Rodrifarias\SlimRouteAttributes\Route\Scan\ScanRoutes; $pathDirControllers = __DIR__ . '/your-dir'; $app = AppSlimFactory::create(); $app->registerRoutes($pathDirControllers, new ScanRoutes()); $app->run();
Creating a controller
<?php use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Rodrifarias\SlimRouteAttributes\Attributes\HttpMethods\Delete; use Rodrifarias\SlimRouteAttributes\Attributes\HttpMethods\Get; use Rodrifarias\SlimRouteAttributes\Attributes\HttpMethods\Patch; use Rodrifarias\SlimRouteAttributes\Attributes\HttpMethods\Post; use Rodrifarias\SlimRouteAttributes\Attributes\HttpMethods\Put; use Rodrifarias\SlimRouteAttributes\Attributes\Middleware; use Rodrifarias\SlimRouteAttributes\Attributes\PublicAccess; use Rodrifarias\SlimRouteAttributes\Attributes\Route; use Rodrifarias\SlimRouteAttributes\Tests\Unit\Route\Middleware\MiddlewareAfter; use Rodrifarias\SlimRouteAttributes\Tests\Unit\Route\Middleware\MiddlewareBefore; #[Route('/')] class HomeController { #[Get, PublicAccess(true)] public function showAll(RequestInterface $request, ResponseInterface $response): ResponseInterface { $response->getBody()->write('Home'); return $response->withHeader('Content-type', 'application/json'); } #[Get('optional[/{id:[0-9]+}]')] public function optional(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface { $id = $args['id'] ?? ''; $response->getBody()->write('Optional' . $id); return $response->withHeader('Content-type', 'application/json'); } #[Get('{id:\d+}'), PublicAccess(true)] public function show(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface { $response->getBody()->write('Hello ' . $args['id']); $response->withHeader('Content-type', 'application/json'); return $response->withStatus(200); } #[Post, Middleware([MiddlewareAfter::class, new MiddlewareBefore()])] public function create(RequestInterface $request, ResponseInterface $response): ResponseInterface { return $response->withHeader('Content-type', 'application/json'); } #[Put('{id:\d+}')] public function update(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface { $response->getBody()->write('Updated ' . $args['id']); return $response->withHeader('Content-type', 'application/json'); } #[Delete('{id:\d+}')] public function delete(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface { $response->withHeader('Content-type', 'application/json'); return $response->withStatus(204); } #[Patch('{id:\d+}')] public function updatePatch(RequestInterface $request, ResponseInterface $response, array $args): ResponseInterface { $response->getBody()->write('Updated Patch ' . $args['id']); return $response->withHeader('Content-type', 'application/json'); } }
You may quickly test this using the built-in PHP server:
$ php -S localhost:8000 -t public
Going to http://localhost:8000 will now display "Home".
Available Http Methods
GET, POST, PUT, DELETE, PATCH
Middleware in Route
To run a middleware you have to add the following attribute (Middleware) in the method
#[Route('/home')] class HomeController { #[Get, PublicAccess(true), Middleware([MiddlewareAfter::class])] public function showAll(RequestInterface $request, ResponseInterface $response): ResponseInterface { $response->getBody()->write('Home'); return $response->withHeader('Content-type', 'application/json'); } }
Map Route
You can use multiples http methods using map
#[Route('/home')] class HomeController { #[Map('/map', ['POST', 'GET'])] public function mapPostGet(RequestInterface $request, ResponseInterface $response): ResponseInterface { $response->getBody()->write('Map With Post and Get'); return $response->withHeader('Content-type', 'application/json'); } }
Command to show all registered routes
$ php vendor/bin/show-routes.php show-routes --path=your-dir
Route | Http Method | Controller Method | IsPublic |
---|---|---|---|
/ | GET | Controller:method | yes |
Get list routes
<?php use Rodrifarias\SlimRouteAttributes\Route\Scan\ScanRoutes; require_once __DIR__ . '/vendor/autoload.php'; $scan = new ScanRoutes(); $routes = $scan->getRoutes(__DIR__ . '/your-dir'); // you can use getRoutes with cache $routes = $scan->getRoutes(__DIR__ . '/your-dir', true); foreach ($routes as $route) { echo $route . PHP_EOL . PHP_EOL; }
Tests
To execute the test suite, you'll need to install all development dependencies.
$ git clone https://github.com/rodrifarias/slim-route-attributes
$ composer install
$ composer test