Trying to figure out error handling and a 404 twig page

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.


        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;


        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->add(new Zeuxisoo\Whoops\Slim\WhoopsMiddleware());
   //    $app->add(ExceptionMiddleware::class);
   //    $app->add(HttpExceptionMiddleware::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.

First, the HTTP specification does not “allow” to set a “Location” response header in a HTTP 404 (Not found) response. If you are trying to redirect the user, the response code 302 and the “Location” response header is needed.

If you already handle the “HttpNotFoundException”, then your need to handle that type of error within that handler. So you need to render the Twig file within that error handler.

Then, do not add the same ErrorMiddleware twice like this:

$errorMiddleware = $app->addErrorMiddleware(true, true, true);

This will not work. So I would remove the $errorMiddleware = $app->addErrorMiddleware(true, true, true); line and configure the ErrorMiddleware within the DI container instead.

Note that, when you use a custom error handler like App\Middleware\ErrorHandlerMiddleware then you may not need to add other middleware handler to the Slim ErrorMiddleware.