. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\Middleware; use Fig\Http\Message\RequestMethodInterface; use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; use Fisharebest\Webtrees\Http\RequestHandlers\HomePage; use Fisharebest\Webtrees\Http\ViewResponseTrait; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use function app; use function redirect; use function route; /** * Middleware to generate a response when no route was matched. */ class NoRouteFound implements MiddlewareInterface { use ViewResponseTrait; /** * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * * @return ResponseInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { // Bind the request into the container. We'll need it to generate an error page. app()->instance(ServerRequestInterface::class, $request); if ($request->getMethod() !== RequestMethodInterface::METHOD_GET) { throw new HttpNotFoundException(); } return redirect(route(HomePage::class)); } }