Hi there.
I am new to Slim 4 framework.
Can anyone help.
I want to implement a “universal” page not found error “handle” from config\container.php that fires up a custom 404 error page. I think the container.php is where this should be fired off every time that a “non” url is entered. I.e. HttpNotFoundException
Using the Slim 4 Framework, and having implemented what I understand is the error handling, I am stumped and cannot see how to fire off a 404 error page in templates\not-found.html.twig. I.e. I see Error Handling and Exception Handling in config\middleware.php below?
I.e. I have set up the middleware etc. to the best of my knowledge per Daniels advice per the manuals found here: eBook Online Platform
I have cancelled some of the directives in config/middleware.php as I believe these may be conflicting.
See my commented lines in config/middleware below.
I am able to receive the message from the config\container.php that I have commented out when I put in an invalid url. I.e. I get the message sent from config/container below: “404 Not Found From the message in container.php”
I am just not sure how to fire off my not-found.html.twig template from the code in the config\container.php below. I thought I could redirect through a controller but this seems a bit iffy. Any advise will really help me.
config\container.php
$errorMiddleware->setErrorHandler(
HttpNotFoundException::class,
function (ServerRequestInterface $request, Throwable $exception, bool $displayErrorDetails) {
$response = new Response();
// $response->getBody()->write('404 Not Found From the message in container.php');
// return $response->withStatus(404);
return $response <<<Is this the best way to fire off twig not found 404 page?
->withHeader('Location', '/notfound') <<<Is this the best way ...twig not found 404 page?
->withStatus(404); <<<Is this the best way to fire off twig not found 404 page?
}
);
return $errorMiddleware;
},
config/middleware.php
<?php
use App\Error\Renderer\HtmlErrorRenderer;
use App\Middleware\HttpExceptionMiddleware;
use App\Middleware\ErrorHandlerMiddleware;
use App\Middleware\ExceptionMiddleware;
use App\Middleware\FlashMessagesMiddleware;
use Selective\BasePath\BasePathMiddleware;
use Slim\App;
use Slim\Middleware\ErrorMiddleware;
use Slim\Views\TwigMiddleware;
return function (App $app) {
$app->addBodyParsingMiddleware();
$app->add(TwigMiddleware::class);
$app->addRoutingMiddleware();
// $app->add(new Zeuxisoo\Whoops\Slim\WhoopsMiddleware());
$app->add(BasePathMiddleware::class);
// $app->add(ExceptionMiddleware::class);
// $app->add(HttpExceptionMiddleware::class);
$app->add(ErrorHandlerMiddleware::class);
$app->add(ErrorMiddleware::class);
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
NOT SURE IF BELOW HELPS: I set myself a goal to use a course on udemy called PHP Microframeworks with Slim written in Slim 3 and to convert it to run on Slim 4 Framework.
GitHub - markjcorrigan/slim4Framework: A fiddle to make fit from Udemy Slim 3 course PHP Microframeworks with Slim 4 framework There is a notes file that gives a quick overview. Apol if my code is uninformed. Trying to get up to speed.