B
B
BonBon Slick2018-08-18 21:44:32
symfony
BonBon Slick, 2018-08-18 21:44:32

Flash messages not working in event subscriber?

docs https: //symfony.com/blog/new-in-symfony-3-3-improv
...
https://symfony.com/doc/current/controller.html
twig

{{ dump(app.flashes()) }}
    {{ dump(app.flashes) }}
    {{ dump(app.flashes('notice')) }}
    {{ dump( app.flashes('notice')) }}
    {{ dump(app.flashes(['notice'])) }}
    {{ dump(app.session.get('notice') ) }}
    {{ dump( app.session.flashbag.all) }}
    {{ dump( app.session.flashbag.all()) }}
    {{ dump(app.session.flashBag.get('notice') ) }}
    {{ dump( app.session.flashBag('notice') ) }}
    {% for msg in  app.flashes %}
        {{ msg }}
    {% endfor %}
    {% for msg in  app.session.flashbag.all() %}
        {{ msg }}
    {% endfor %}
    {% for message in app.flashes('notice') %}
        <div class="flash-notice">
            {{ message }}
        </div>
    {% endfor %}
    {% for message in app.session.flashBag('notice') %}
        <div class="flash-notice">
            {{ message }}
        </div>
    {% endfor %}

    {% for label, messages in app.flashes %}
        {% for message in messages %}
            {{ message }}
        {% endfor %}
    {% endfor %}

subscriber
{
        return [
            KernelEvents::EXCEPTION => [
                ['test', 0],
            ],
        ];
    }

    public function test(GetResponseForExceptionEvent $event) 
    {
        $this->session->getFlashBag()->add(
            'notice',
            'Your changes were saved!'
        );

result
5b7868cd7c056174255339.png
As you can see, it doesn't work, but if I issue it from the controller, then everything works as it should.
Apparently, the listener works after the template is rendered, because if you add a check for null to Twig, nothing will be output at all no matter what you check.
Tried to increase the priority of the listener, still the same. Naturally, any other checks for, if there is a message to display, do not work out, because. when the output of the html message occurs, it is not there yet.
Initially, I worked with session.set and session.remove, there were difficulties, it is not clear where, when and what to clean, and the flashback is reset when the page is refreshed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2018-08-19
@BonBonSlick

Messages are deleted after they are received
https://github.com/symfony/symfony/blob/master/src...
https://github.com/symfony/symfony/blob/master/src...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question