xref: /webtrees/app/Http/Middleware/NoRouteFound.php (revision d35568b467207589ea9059739da0bf1f7e785a0d)
1a22c26f3SGreg Roach<?php
23976b470SGreg Roach
3a22c26f3SGreg Roach/**
4a22c26f3SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6a22c26f3SGreg Roach * This program is free software: you can redistribute it and/or modify
7a22c26f3SGreg Roach * it under the terms of the GNU General Public License as published by
8a22c26f3SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a22c26f3SGreg Roach * (at your option) any later version.
10a22c26f3SGreg Roach * This program is distributed in the hope that it will be useful,
11a22c26f3SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a22c26f3SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a22c26f3SGreg Roach * GNU General Public License for more details.
14a22c26f3SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a22c26f3SGreg Roach */
17fcfa147eSGreg Roach
18a22c26f3SGreg Roachdeclare(strict_types=1);
19a22c26f3SGreg Roach
20a22c26f3SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
21a22c26f3SGreg Roach
22ee4364daSGreg Roachuse Fig\Http\Message\RequestMethodInterface;
2381b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
240c0910bfSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\HomePage;
25a22c26f3SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
26*d35568b4SGreg Roachuse Fisharebest\Webtrees\Registry;
27a22c26f3SGreg Roachuse Psr\Http\Message\ResponseInterface;
28a22c26f3SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
29a22c26f3SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
30a22c26f3SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
313976b470SGreg Roach
32a22c26f3SGreg Roachuse function redirect;
33a22c26f3SGreg Roachuse function route;
34a22c26f3SGreg Roach
35a22c26f3SGreg Roach/**
36a22c26f3SGreg Roach * Middleware to generate a response when no route was matched.
37a22c26f3SGreg Roach */
3871378461SGreg Roachclass NoRouteFound implements MiddlewareInterface
39a22c26f3SGreg Roach{
40a22c26f3SGreg Roach    use ViewResponseTrait;
41a22c26f3SGreg Roach
42a22c26f3SGreg Roach    /**
43a22c26f3SGreg Roach     * @param ServerRequestInterface  $request
44a22c26f3SGreg Roach     * @param RequestHandlerInterface $handler
45a22c26f3SGreg Roach     *
46a22c26f3SGreg Roach     * @return ResponseInterface
47a22c26f3SGreg Roach     */
48a22c26f3SGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
49a22c26f3SGreg Roach    {
50f307429bSGreg Roach        // Bind the request into the container.  We'll need it to generate an error page.
51*d35568b4SGreg Roach        Registry::container()->set(ServerRequestInterface::class, $request);
52f307429bSGreg Roach
5371378461SGreg Roach        if ($request->getMethod() !== RequestMethodInterface::METHOD_GET) {
54d501c45dSGreg Roach            throw new HttpNotFoundException();
55ee4364daSGreg Roach        }
56a22c26f3SGreg Roach
570c0910bfSGreg Roach        return redirect(route(HomePage::class));
58ee4364daSGreg Roach    }
59a22c26f3SGreg Roach}
60