spiral/annotated-routes

Annotated routes for Spiral Framework

v2.8.9 2021-04-28 09:30 UTC

README

Latest Stable Version Build Status Codecov

Provides the ability to use annotated routes and route groups.

Installation:

To install the component:

$ composer require spiral/annotated-routes

Activate the bootloader in your application (instead of RouteBootloader):

use Spiral\Router\Bootloader as Router;

// ...

protected const LOAD = [
    // ...
    Router\AnnotatedRoutesBootloader::class,
    // ...
];

Configuration:

Define one or multiple route groups (optional) or configure default group in your bootloader:

namespace App\Bootloader;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Router\GroupRegistry;

class MiddlewareBootloader extends Bootloader
{
    public function boot(GroupRegistry $registry)
    {
        $registry
            ->getGroup('api')
            ->setPrefix('api/v1')
            ->addMiddleware(App\Security\ApiAuthMiddleware::class);
    }
}

Use environment variable ROUTE_CACHE to enable or disable route caching (by default enabled in non DEBUG mode). To reset route cache:

$ php app.php route:reset

Usage:

Use the annotation in your controller methods:

namespace App\Controller;

use Spiral\Router\Annotation\Route;

class HomeController
{
    /**
     * @Route(route="/", name="home", methods="GET", group="api")
     */
    public function index()
    {
        return 'Hello World';
    }
}

Use group attribute to configure middleware and domain core in bulk.

License:

MIT License (MIT). Please see LICENSE for more information. Maintained by Spiral Scout.