MyShaarli/application/front/controller/visitor/ErrorNotFoundController.php
ArthurHoaro d52ab0b1e9 Properly handle 404 errors
Use 404 template instead of default Slim error page if the route is not found.

Fixes #827
2020-09-12 12:42:19 +02:00

29 lines
834 B
PHP

<?php
declare(strict_types=1);
namespace Shaarli\Front\Controller\Visitor;
use Slim\Http\Request;
use Slim\Http\Response;
/**
* Controller used to render the 404 error page.
*/
class ErrorNotFoundController extends ShaarliVisitorController
{
public function __invoke(Request $request, Response $response): Response
{
// Request from the API
if (false !== strpos($request->getRequestTarget(), '/api/v1')) {
return $response->withStatus(404);
}
// This is required because the middleware is ignored if the route is not found.
$this->container->basePath = rtrim($request->getUri()->getBasePath(), '/');
$this->assignView('error_message', t('Requested page could not be found.'));
return $response->withStatus(404)->write($this->render('404'));
}
}