sunrise / http-router-annotations-support
Annotations support for Sunrise HTTP Router
Installs: 9 563
Dependents: 1
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 1
Open Issues: 3
Requires
- php: ^7.1
- doctrine/annotations: ^1.6
- sunrise/http-router: ^1.0
Requires (Dev)
- phpunit/phpunit: 7.5.11
- psr/http-message: 1.0.1
- psr/http-server-handler: 1.0.1
- psr/http-server-middleware: 1.0.1
This package is auto-updated.
Last update: 2021-02-23 14:58:49 UTC
README
Installation (via composer)
composer require sunrise/http-router-annotations-support
How to use?
QuickStart
$router = new \Sunrise\Http\Router\Router(); $loader = new \Sunrise\Http\Router\AnnotationRouteLoader(); $routes = $loader->load(__DIR__ . '/src/Http/Controller/Foo'); $router->addRoutes($routes); $routes = $loader->load(__DIR__ . '/src/Http/Controller/Bar'); $router->addRoutes($routes); $response = $router->handle(...);
ExampleController
declare(strict_types=1); namespace App\Http\Controller; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; /** * @Route( * id="resource.update", * path="/resource/{id<\d+>}", * methods={"PATCH"}, * before={ * "App\Http\Middleware\FooMiddleware", * "App\Http\Middleware\BarMiddleware" * }, * after={ * "App\Http\Middleware\BazMiddleware", * "App\Http\Middleware\QuxMiddleware" * } * ) */ class ResourceUpdate implements MiddlewareInterface { public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ) : ResponseInterface { $response = $handler->handle($request); $resourceId = $request->getAttribute('id'); // some code... return $response; } }
Test run
php vendor/bin/phpunit