marekskopal/router

Attributes extension for league/route router

v1.1.0 2024-11-18 21:19 UTC

This package is auto-updated.

Last update: 2024-12-18 21:33:45 UTC


README

Install

composer require marekskopal/router

Usage

Create Router instance with RouterBuilder and set class directories of your controllers or actions. Optionally you can set CacheInterface instance for caching routes.

use MarekSkopal\Router\Builder\RouterBuilder;

$router = (new RouterBuilder())
    ->setClassDirectories([__DIR__ . '/../Controllers'])
    ->setCache(new Cache()) // optional
    ->build();

Add Route (or RouteGet,RoutePost,RoutePut,RouteDelete...) attribute on Class or Method you want to route to.

use MarekSkopal\Router\Attribute\Route;
use MarekSkopal\Router\Attribute\RoutePost;

class MyController
{
    #[Route('GET', '/api/my/name')]
    public function getName(): void
    {
    }
    
    #[RoutePost('/api/my/address')]
    public function postAddress(): void
    {
    }
}
use MarekSkopal\Router\Attribute\RouteGet;

#[RouteGet('/api/my/action')]
class MyAction
{
    public function __invoke(): void
    {
    }
}