How to put data in the session flash when redirecting to Silex?

Asked

Viewed 52 times

1

Some frameworks have a feature called Session Flash, where it is possible to store a certain value in the session and when it is accessed, it is immediately removed from it, which is useful to show error messages in certain requests.

I know how to do it in frameworks like Laravel and Cakephp, but how could I do it in Silex?

I need to store a session flash value to display it after a redirect, using Silex microframework, but I don’t know how.

Sample code:

$app->get('/rota', function ()  use($app){

    // Quero enviar "mensagem" com o valor "Cadastrado com sucesso aqui" num flash

    return $app->redirect('/outra/rota');
});

1 answer

2


According to this example, you must register SessionServiceProvider:

$app->register(new Silex\Provider\SessionServiceProvider());

After registering the functionality you can already:

$app->get('/whatever', function() use($app) { 
  # Definir mensagem e nome da mensagem (example)
  $app['session']->getFlashBag()->add('example', 'Some example flash message');
  return $app->redirect('redirect-to-some-route');
});

And finally if you are using Twig template system, to make the same display(s)):

{% for alert in app.session.flashbag.get('example') %}
<div class="error-message">
  <div class="alert"><strong>{{ alert }}</strong></div>
</div>
{% endfor %}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.