Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show flash message without redirect (eg forward). Messages shown twice

Is it possible to show flash messages in Symfony 2 without redirect? Or editing core files in another possible solution on 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

Oh actually I noticed if I just used a forward, flash messages will show, but it will still show on next request

like image 902
Jiew Meng Avatar asked Jan 06 '12 06:01

Jiew Meng


3 Answers

Why using flashes if you don't want them to be persisted until next request ?

Couldn't you find other ways to display feedback like template parameters ?

If not, you could add this in your templates (according you are displaying flashes like 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 redirection will remove them from session before returning the response. I think it's the last better solution.

like image 197
Florian Klein Avatar answered Sep 20 '22 23:09

Florian Klein


I will speak in general terms, because you want something that is not a ready standard.

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

You need to call an action script using an ajax request, serializing the data of a form, getting the message to return and then display it the way it server you better.

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

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

The return from saving an item with ajax is different, in this case i´m encoding the data using JSON, so, if you want to perfom another actions you can inject more variables in the JSON array, manipulating them inside function(data) { ... }

Hope that helps

like image 45
Guilherme Viebig Avatar answered Sep 21 '22 23:09

Guilherme Viebig


Flash messages have been created in order to live through the redirection you usually do after a form submission success. That's why they live for 2 requests.

If you want to manage soem user notifications in your app, make a controller managing a notification TWIG block in your layout. This block can embed flash messages AND these notifications you handle with the controller. You'll have to set them in your session, remove them in your NotificationController and it's alright...

like image 39
AlterPHP Avatar answered Sep 21 '22 23:09

AlterPHP