for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Psr7Middlewares\Middleware;
use Psr7Middlewares\Utils;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use FastRoute\Dispatcher;
class FastRoute
{
use Utils\CallableTrait;
/**
* @var Dispatcher FastRoute dispatcher
*/
private $router;
* Set the Dispatcher instance.
*
* @param Dispatcher|null $router
public function __construct(Dispatcher $router)
$this->router = $router;
}
* Execute the middleware.
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
* @return ResponseInterface
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
$route = $this->router->dispatch($request->getMethod(), $request->getUri()->getPath());
if ($route[0] === Dispatcher::NOT_FOUND) {
return $response->withStatus(404);
if ($route[0] === Dispatcher::METHOD_NOT_ALLOWED) {
return $response->withStatus(405);
foreach ($route[2] as $name => $value) {
$request = $request->withAttribute($name, $value);
$response = $this->executeCallable($route[1], $request, $response);
return $next($request, $response);