W3docs

Symfony2: Redirecting to last route and flash a message?

In Symfony2, you can redirect to the last route using the redirect function and passing in the referer option:

In modern Symfony, you can redirect to the previous route using the redirect method and passing the referer header directly:

Example of redirecting to the last route in Symfony

return $this->redirect($request->headers->get('referer'));

To flash a message, you can use the addFlash method:

Example of setting a flash message in Symfony

$this->addFlash('notice', 'Your message here');

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

You can then retrieve the flash message in your template using the flashes function:

Example of retrieving the flash message in the template

{% for message in app.flashes('notice') %}
    <div class="flash-notice">
        {{ message }}
    </div>
{% endfor %}

You can also use the addFlash() helper method directly in your controller.

You can then retrieve the flash message in your template using the flashes function as shown above.