Silex: Redirect with Flash Data

Mike Rockétt picture Mike Rockétt · Sep 30, 2013 · Viewed 7.5k times · Source

I need to redirect one page to another with a message in Silex. Hopefully there's a Laravelesque way of doing it, but I highly doubt it:

$app->redirect('/here', 301)->with('message', 'text');

I'd then want to display the message in my template:

{{ message }}

If not, is there another way?

Update

I see there's a getFlashBag method in Symfony - is that what I'm supposed to use? Specifically, I am using the Bolt Content Management system.

Answer

a4c8b picture a4c8b · Sep 30, 2013

Yes, FlashBag is the right way. Set a flash message in your controller (you can add multiple messages):

$app['session']->getFlashBag()->add('message', 'text');
$app->redirect('/here', 301)

And print it in the template:

{% for message in app.session.getFlashBag.get('message') %}
    {{ message }}
{% endfor %}