0

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

Qualussi
  • 1
  • 1
  • I am not sure how it implemented in symfony but probably they use output buffer control to prevent all unnecessary output from showing, and only show data that returned from function. So you need to use some variable instead dump, and pass it same as you passed `'exception' => $exception` as second item in array, and then output it in twig. – Sergey92zp Jun 02 '23 at 19:59
  • instead of Request you could DI the [RequestStack](https://github.com/symfony/symfony/blob/6.3/src/Symfony/Component/HttpFoundation/RequestStack.php) instead off which you can access the main or parent request – Jakumi Jun 03 '23 at 04:51
  • @Sergey92zp The question was badly asked, it's not that the dump isn't working, it's rather that it's displaying null. I've edited my post. – Qualussi Jun 05 '23 at 06:29
  • @Jakumi Thanks to `RequestStack`, I can indeed access the previous request, but I still can't access the `FlashBag` because it's managed by the PHP session. So when it's deleted, it's deleted for good and for all past and future requests. There are now two possibilities, it seems to me: 1. Use the previous request to store the information in the form of `attributes`. 2. Make my own `FlashBag` and save the info in the session and delete it once in the `ErrorController`. – Qualussi Jun 05 '23 at 06:37
  • then probably it is some sub-request, you can request flash message directly in your `commonoffice/error/test.html.twig` via `app.flashes('info')` View renders only once, so it will not request it twice. About exception you can render simple view with custom response code like this https://stackoverflow.com/a/28883188/2590591 – Sergey92zp Jun 05 '23 at 11:46
  • Since I'm slightly confused ... does the second dump still return `null` or now `[]`? if the latter: are you aware, that `FlashBag::all()` will *clear* the bag and `FlashBag::peekAll()` should be used instead? (just covering the bases, no offense). Because I would have imagined, that the session object, including all its bags, should still exist during the sub-request. I might be wrong though.... – Jakumi Jun 05 '23 at 16:11

0 Answers0