logifire/nano-router

This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

Router implementation using PSR-7

4.0.1 2020-06-30 14:46 UTC

This package is auto-updated.

Last update: 2021-07-10 18:06:05 UTC


README

Usage

Basic

    // Controller implementation
    class StringController implements Controller {
        public function run(): ResponseInterface {
            ...
        }
    }

    ...
    // Router configuration
    $router = new Router();
    $router->configurePath(Router::METHOD_GET, '/profiles/(?<uuid>[0-9a-f\-]{36})', StringController::class);
    $router->configurePath(Router::METHOD_GET, '/profiles/(?<id>\d+)', IntegerController::class);

    ...
    // Request handling
    $router_result = $router->processRequest($server_request);

    if ($router_result !== null) {
        $controller_name = $router_result->getControllerName();
        $path_result = $router_result->getPathResult();
        $query_result = $router_result->getQueryResult();

        $controller = new $controller_name({dependencies});

        $psr7_response = $controller->buildResponse();
    }
    ... 

This package comes with PSR-15 RouterMiddleware