I would like to access to flash message in custom framework.error_controller
in Symfony (5.4).
I have a HomeController where I set this:
$request->getSession()->getFlashBag()->set('info','This is a test')
dump($request->getSession()->getFlashBag()->all());
throw new BadRequestException("error.bad.request.accueil");
Of course here the dump
works fine.
Here is a part of framework.yaml
:
framework:
error_controller: App\Controller\CommonOffice\ErrorController::show
And here is the show function for the ErrorController
public function show(\Throwable $exception, Request $request): Response
{
dump($request->getSession()->getFlashBag()->all())
if ($request->headers->get('referer')) dump($exception, $request);
return $this->render("commonoffice/error/test.html.twig", [
'exception' => $exception
]);
}
And now the dump
display null
.
I've more or less understood that exceptions can sometimes create sub-requests which are probably the cause of the fact that I can't access flash messages.
If this is the case, is there anything I can do to change this behaviour?
I want to pass information from my HomeController to the ErrorController, is there any way of doing this (apart from using the exception message)?
I use an exception because it allows me to return a response with the correct HTTP code and I find it cleaner, but it may not be the best way in the end.
On the page, I want to display the exception message plus some “advices” to explain why it doesn't work (the flashbag with the 'info' key contains these “advices”).
I hope to be clear.
I have tried using the attributes of the request, accessing the session and flashbag directly via autowiring, but it doesn't change anything