diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-09-12 12:42:19 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-09-12 12:42:19 +0200 |
commit | d52ab0b1e99aa0c494f389092dce1e926296032d (patch) | |
tree | 3a436eedf30cbb8f8b0943c5e5c3b88e49488b69 /application/front/controller | |
parent | e2dff28b44fafcf11a1db7985c50cd40e6945821 (diff) | |
download | Shaarli-d52ab0b1e99aa0c494f389092dce1e926296032d.tar.gz Shaarli-d52ab0b1e99aa0c494f389092dce1e926296032d.tar.zst Shaarli-d52ab0b1e99aa0c494f389092dce1e926296032d.zip |
Properly handle 404 errors
Use 404 template instead of default Slim error page if the route is not found.
Fixes #827
Diffstat (limited to 'application/front/controller')
-rw-r--r-- | application/front/controller/visitor/ErrorNotFoundController.php | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/application/front/controller/visitor/ErrorNotFoundController.php b/application/front/controller/visitor/ErrorNotFoundController.php new file mode 100644 index 00000000..758dd83b --- /dev/null +++ b/application/front/controller/visitor/ErrorNotFoundController.php | |||
@@ -0,0 +1,29 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Visitor; | ||
6 | |||
7 | use Slim\Http\Request; | ||
8 | use Slim\Http\Response; | ||
9 | |||
10 | /** | ||
11 | * Controller used to render the 404 error page. | ||
12 | */ | ||
13 | class ErrorNotFoundController extends ShaarliVisitorController | ||
14 | { | ||
15 | public function __invoke(Request $request, Response $response): Response | ||
16 | { | ||
17 | // Request from the API | ||
18 | if (false !== strpos($request->getRequestTarget(), '/api/v1')) { | ||
19 | return $response->withStatus(404); | ||
20 | } | ||
21 | |||
22 | // This is required because the middleware is ignored if the route is not found. | ||
23 | $this->container->basePath = rtrim($request->getUri()->getBasePath(), '/'); | ||
24 | |||
25 | $this->assignView('error_message', t('Requested page could not be found.')); | ||
26 | |||
27 | return $response->withStatus(404)->write($this->render('404')); | ||
28 | } | ||
29 | } | ||