Show flash message without redirection (for example, forward). Messages shown twice

Is it possible to display flash messages in Symfony 2 without redirecting? Or editing core files in another possible solution in google groups?

//Symfony\Component\HttpFoundation\Session public function setFlash($name, $value, $persist = true) { if (false === $this->started) { $this->start(); } $this->flashes[$name] = $value; if($persist) { unset($this->oldFlashes[$name]); } else { $this->oldFlashes[$name] = $value; } } 

UPDATE

In fact, I noticed that if I just used forwards, flash messages will show, but it will be displayed on the next request

+7
source share
3 answers

Why use flashes if you do not want them to persist until the next request?

Could you find other ways to display feedback, such as template parameters?

If not, you can add this to your templates (according to what you show the flashes as shown below):

 {% if app.session.hasFlash('notice') %} <div class="flash-notice"> {{ app.session.flash('notice') }} {{ app.session.removeFlash('notice') }} </div> {% endif %} 

So that any template that displays these flashes before redirecting will remove them from the session before returning the response. I think this is the last best solution.

+7
source

I will talk in general terms because you want something that is not a ready-made standard.

At this time, after the request, Symfony flash messages appear, they are in this way, and to save it you need to find an alternative, assign an ajax call.

You need to invoke the script action with an ajax request, serialize the form data, return the message, and then display it in the way you like.

I used http://jquery.bassistance.de/message/demo/ along with this sample jquery call in some projects, it works well:

  $.post("/product/saveAjax", { $("#product").serialize() }, function(data){ $().message(data.message); }, "json"); 

The return from saving the element using ajax is different, in this case I encode the data using JSON, so if you want to perform other actions, you can enter more variables into the JSON array by manipulating them inside the function (data) {...}

Hope that helps

+3
source

Flash messages were created in order to live through the redirection that you usually do after the success of submitting a form. That is why they live for 2 requests.

If you want to manage soem user notifications in your application, make a controller that controls the TWIG notification block in your layout. This block can insert flash messages and these notifications that you process using the controller. You will need to install them in your session, delete them in the NotificationController, and everything is fine ...

+1
source

All Articles