We will use Route from The League of Extraordinary Packages (a PHP developer group) so that we have a PSR-7 routing system and dispatch our PSR-15 middleware on it. In short, Route is a fast PSR-7 routing/dispatcher package.
It is a PSR-15 server request handler and can handle the invocation of a stack of middleware. It is built on top of FastRoute (https://github.com/nikic/FastRoute) by Nikita Popov.
Let's learn how to get it hooked up to the app:
- Install league/route via Composer:
$ composer require league/route
- Once you have it installed, we can refactor our "Hello World" component with a route, as follows:
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
$request = Zend\Diactoros\ServerRequestFactory::fromGlobals(
//...
);
$router = new League\Route\Router;
$router->map('GET', '/', function (ServerRequestInterface $request) : ResponseInterface {
$response = new Zend\Diactoros...