Skip to content

Router

Raxos Router is a fast, attribute-based router for PHP 8.5. Controllers are plain classes annotated with attributes such as #[Controller] and #[Get]. The router reflects them once into a compiled set of static and dynamic routes, then resolves an HttpRequest into an HttpResponse by running a per-route pipeline of middleware and the target method. Path, query and header values, ORM models, and container services are all injected automatically based on parameter types and mapping attributes. A closure-based DynamicRouter offers a lighter alternative when full controller classes are not needed.

Highlights

Explore by category

Quick example

php
<?php
declare(strict_types=1);

namespace App\Http\Controller;

use Raxos\Router\Attribute\{Controller, Get};

#[Controller('/todos')]
final readonly class TodoController
{
    #[Get('/')]
    public function index(): array
    {
        return ['todos' => []];
    }

    #[Get('/$id')]
    public function show(int $id): array
    {
        return ['id' => $id];
    }
}
php
<?php
declare(strict_types=1);

use App\Http\Controller\TodoController;
use Raxos\Container\Container;
use Raxos\Http\HttpRequest;
use Raxos\Router\Router;

$container = new Container(production: true);
$router = Router::createFromControllers($container, [
    TodoController::class,
]);

$router->resolve(HttpRequest::create())->send();

Installation

Install it with Composer.

shell
composer require raxos/router

See installation for requirements, or use the sidebar to navigate this package.