jasny/router

This package is abandoned and no longer maintained. The author suggests using the jasny/switch-route package instead.

A versatile router for PHP

v1.1.3 2020-01-16 15:40 UTC

README

Build Status Scrutinizer Code Quality Code Coverage SensioLabsInsight Packagist Stable Version Packagist License

Jasny Router is a versatile PSR-7 compatible router. It decouples the way to determine a route, from the routing and from running the routed action. The router supports double pass middleware.

Installation

The Jasny Router package is available on packagist. Install it using composer:

composer require jasny/router

Basic Usage

use Jasny\Router;
use Jasny\Router\Routes\Glob as Routes;
use Jasny\HttpMessage\ServerRequest;
use Jasny\HttpMessage\Response;

$routes = new Routes([
    '/' => function($request, $response) {
        $response->getBody()->write('Hello world');
        return $response;
    },
]);

$router = new Router($routes);
$router->handle(new ServerRequest()->withGlobalEnvironment(), new Response());

Routes

When creating a Router, you need to pass a object that implements the RoutesInterface. Routes should be seen as a collection of routes, with the ability to select one of those routes based on the server request.