diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-09-12 21:41:58 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-12 21:41:58 +0200 |
commit | 0d930454a2892715e691f9c7713e26a3bb4ee96c (patch) | |
tree | 770197f23a6ec512f54bd22578ec971f53408dc3 /application | |
parent | 4af591ff3c5db4dea5b6c437527f6f9b12917570 (diff) | |
parent | d52ab0b1e99aa0c494f389092dce1e926296032d (diff) | |
download | Shaarli-0d930454a2892715e691f9c7713e26a3bb4ee96c.tar.gz Shaarli-0d930454a2892715e691f9c7713e26a3bb4ee96c.tar.zst Shaarli-0d930454a2892715e691f9c7713e26a3bb4ee96c.zip |
Merge pull request #1553 from ArthurHoaro/fix/404-page
Properly handle 404 errors
Diffstat (limited to 'application')
-rw-r--r-- | application/container/ContainerBuilder.php | 4 | ||||
-rw-r--r-- | application/container/ShaarliContainer.php | 9 | ||||
-rw-r--r-- | application/front/controller/visitor/ErrorNotFoundController.php | 29 |
3 files changed, 38 insertions, 4 deletions
diff --git a/application/container/ContainerBuilder.php b/application/container/ContainerBuilder.php index 58067c99..55bb51b5 100644 --- a/application/container/ContainerBuilder.php +++ b/application/container/ContainerBuilder.php | |||
@@ -10,6 +10,7 @@ use Shaarli\Config\ConfigManager; | |||
10 | use Shaarli\Feed\FeedBuilder; | 10 | use Shaarli\Feed\FeedBuilder; |
11 | use Shaarli\Formatter\FormatterFactory; | 11 | use Shaarli\Formatter\FormatterFactory; |
12 | use Shaarli\Front\Controller\Visitor\ErrorController; | 12 | use Shaarli\Front\Controller\Visitor\ErrorController; |
13 | use Shaarli\Front\Controller\Visitor\ErrorNotFoundController; | ||
13 | use Shaarli\History; | 14 | use Shaarli\History; |
14 | use Shaarli\Http\HttpAccess; | 15 | use Shaarli\Http\HttpAccess; |
15 | use Shaarli\Netscape\NetscapeBookmarkUtils; | 16 | use Shaarli\Netscape\NetscapeBookmarkUtils; |
@@ -149,6 +150,9 @@ class ContainerBuilder | |||
149 | ); | 150 | ); |
150 | }; | 151 | }; |
151 | 152 | ||
153 | $container['notFoundHandler'] = function (ShaarliContainer $container): ErrorNotFoundController { | ||
154 | return new ErrorNotFoundController($container); | ||
155 | }; | ||
152 | $container['errorHandler'] = function (ShaarliContainer $container): ErrorController { | 156 | $container['errorHandler'] = function (ShaarliContainer $container): ErrorController { |
153 | return new ErrorController($container); | 157 | return new ErrorController($container); |
154 | }; | 158 | }; |
diff --git a/application/container/ShaarliContainer.php b/application/container/ShaarliContainer.php index 9a9a974a..66e669aa 100644 --- a/application/container/ShaarliContainer.php +++ b/application/container/ShaarliContainer.php | |||
@@ -24,21 +24,22 @@ use Slim\Container; | |||
24 | /** | 24 | /** |
25 | * Extension of Slim container to document the injected objects. | 25 | * Extension of Slim container to document the injected objects. |
26 | * | 26 | * |
27 | * @property string $basePath Shaarli's instance base path (e.g. `/shaarli/`) | 27 | * @property string $basePath Shaarli's instance base path (e.g. `/shaarli/`) |
28 | * @property BookmarkServiceInterface $bookmarkService | 28 | * @property BookmarkServiceInterface $bookmarkService |
29 | * @property CookieManager $cookieManager | 29 | * @property CookieManager $cookieManager |
30 | * @property ConfigManager $conf | 30 | * @property ConfigManager $conf |
31 | * @property mixed[] $environment $_SERVER automatically injected by Slim | 31 | * @property mixed[] $environment $_SERVER automatically injected by Slim |
32 | * @property callable $errorHandler Overrides default Slim exception display | 32 | * @property callable $errorHandler Overrides default Slim exception display |
33 | * @property FeedBuilder $feedBuilder | 33 | * @property FeedBuilder $feedBuilder |
34 | * @property FormatterFactory $formatterFactory | 34 | * @property FormatterFactory $formatterFactory |
35 | * @property History $history | 35 | * @property History $history |
36 | * @property HttpAccess $httpAccess | 36 | * @property HttpAccess $httpAccess |
37 | * @property LoginManager $loginManager | 37 | * @property LoginManager $loginManager |
38 | * @property NetscapeBookmarkUtils $netscapeBookmarkUtils | 38 | * @property NetscapeBookmarkUtils $netscapeBookmarkUtils |
39 | * @property callable $notFoundHandler Overrides default Slim exception display | ||
39 | * @property PageBuilder $pageBuilder | 40 | * @property PageBuilder $pageBuilder |
40 | * @property PageCacheManager $pageCacheManager | 41 | * @property PageCacheManager $pageCacheManager |
41 | * @property callable $phpErrorHandler Overrides default Slim PHP error display | 42 | * @property callable $phpErrorHandler Overrides default Slim PHP error display |
42 | * @property PluginManager $pluginManager | 43 | * @property PluginManager $pluginManager |
43 | * @property SessionManager $sessionManager | 44 | * @property SessionManager $sessionManager |
44 | * @property Thumbnailer $thumbnailer | 45 | * @property Thumbnailer $thumbnailer |
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 | } | ||