How to put data in the session flash when the redirection is done in Silex?

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, is immediately removed from the session, which is useful for displaying error messages on certain requests. >

I can do this in frameworks like Laravel and CakePHP, but how could I do this in Silex?

I need to store a value as a session flash to display it after a redirect, using the Silex microframework, but I do not 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');
});
    
asked by anonymous 18.04.2017 / 18:11

1 answer

1

According to this example , you have register SessionServiceProvider :

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

Once you register the feature you can:

$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 a template twig system, to make the display of the same one:

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