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:
return $this->redirect($this->generateUrl('your_route'), 301, array(), true);
To flash a message, you can use the session
service to set a flash message:
$this->get('session')->getFlashBag()->add('notice', 'Your message here');
Watch a video course
Learn object oriented PHP
You can then retrieve the flash message in your template using the flashes
function:
{% for message in app.session.flashbag.get('notice') %}
<div class="flash-notice">
{{ message }}
</div>
{% endfor %}
You can also use flash()
helper function from Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface
$this->addFlash('notice', 'Your message here');
You can then retrieve the flash message in your template using the flashes
function as above.