Execute middleware after route

Hi @odan, thanks for your answer, with the “slim/message” it’s working well for my controllers and views.
But I have another problem, in some routes I call a middleware for specific route, for auth the user like this:

$app->get('/admin', App\Controllers\AdminController::class.':index')->setName('admin')->add(new AuthMiddleware(['ADMIN']));

In the AuthMiddleware.php I check if the user has the right roles, and if the roles are wrong for the user, I redirect the user to login page with a flash message like this :

class AuthMiddleware extends Middleware
{
    private $tabLevels;

    public function __construct(array $tabLevels = [])
    {
        $this->tabLevels = $tabLevels;
    }

    public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): Response
    {

        $response = $handler->handle($request);

        // My code for check roles


        $this->container->get('flash')->addMessage('errorMessage', $errorMessage);;
        return $response->withHeader('Location', '/login')->withStatus(302);
    }
}

and in my Middleware.php class:

class Middleware
{
    public $container;

    public function __construct(Container $container)
    {
        $this->container = $container;
    }
}

But I get an error “Call to a member function get() on null” for the file AuthMiddleware.php, but In the my Controllers class I have the same type of code (and extend to Controller for get the container) and it’s working well, so I don’t understand why I get this error ?

Thanks for your help