innmind/router

HTTP router

4.1.0 2023-11-26 10:26 UTC

This package is auto-updated.

Last update: 2024-11-26 16:05:29 UTC


README

codecov Build Status Type Coverage

Simple router using url templates as route patterns.

Installation

composer require innmind/router

Usage

use Innmind\Router\{
    Route,
    Route\Name,
    RequestMatcher\RequestMatcher,
    UrlGenerator\UrlGenerator,
};
use Innmind\Http\{
    Method,
    ServerRequest,
};
use Innmind\UrlTemplate\Template;
use Innmind\Url\Url;
use Innmind\Immutable\{
    Sequence,
    Maybe,
};

$routes = Sequence::of(
    Route::of(
        Method::post,
        Template::of('/url{/template}'),
    )->named(Name::of('routeName')),
    Route::of(
        Method::delete,
        Template::of('/resource/{id}'),
    )->named(Name::of('anotherRoute')),
);

$requestMatcher = new RequestMatcher($routes);
$route = $requestMatcher(/* instance of ServerRequest */); // Maybe<Route>
$urlGenerator = new UrlGenerator($routes);
$urlGenerator(Name::of('routeName')); // Url or throws NoMatchingRouteFound

Building a simple app

Example using the innmind/http-server package to respond with files stored in a private folder.

use Innmind\Router\{
    Route,
    RequestMatcher\RequestMatcher,
};
use Innmind\HttpServer\Main;
use Innmind\OperatingSystem\OperatingSystem;
use Innmind\Http\{
    Method,
    ServerRequest,
    Response,
    Response\StatusCode,
};
use Innmind\Filesystem\Name;
use Innmind\UrlTemplate\Template;
use Innmind\Url\Path;

new class extends Main {
    private RequestMatcher $router;

    protected function preload(OperatingSystem $os): void
    {
        $routes = Sequence::of(
            Route::of(Method::get, Template::of('/image/{name}'))->handle(
                fn($request, $variables) => $this->loadFile(
                    $os,
                    $variables->get('name'),
                ),
            ),
            Route::of(Method::get, Template::of('/image/random'))->handle(
                fn($request) => $this->loadFile(
                    $os,
                    generateRandomName(),
                ),
            ),
        );

        $this->router = new RequestMatcher($routes);
    }

    protected function main(ServerRequest $request): Response
    {
        return ($this->router)($request)->match(
            static fn($route) => $route->respondTo($request),
            static fn() => Response::of(
                StatusCode::notFound,
                $request->protocolVersion(),
            ),
        );
    }

    private function loadFile(OperatingSystem $os, string $name): Response
    {
        return $os
            ->filesystem()
            ->mount(Path::of('some/private/folder/'))
            ->get(Name::of($name))
            ->match(
                static fn($file) => Response::of(
                    StatusCode::ok,
                    $request->protocolVersion(),
                    null,
                    $file->content(),
                ),
                static fn() => Response::of(
                    StatusCode::notFound,
                    $request->protocolVersion(),
                ),
            );
    }
}