Flash message issues

Hi all,

Looking for some help :) I’m very new to Yii so bear with me. One of the first things I have been trying to do is add “flash messages”. I guess this has been asked a million times before but I’ve had no luck after Googling/searching the forum.

I have read and implemented this: http://www.yiiframework.com/wiki/21/how-to-work-with-flash-messages/

So in the controller I have


Yii::app()->user->setFlash('success', 'New customer added');

$this->redirect(array('customers/create'));



I added code to layouts -> main.php to look for (and print) flash messages but the message is not being displayed after the page redirects. Also tried adding code in the individual view but no change.

I have also added…


'autoUpdateFlash' => false

… to config -> main.php -> components but this has made no difference.

Does anyone have any suggestions as to what could be causing the problem? Is there someone I am missing? Using 1.1.10.

Thanks.

There has to be some code to display the flash messages in your view, like this:


foreach(Yii::app()->user->getFlashes() as $key => $message) {

        echo '<div class="flash-' . $key . '">' . $message . "</div>\n";

    }

Check this http://www.yiiframework.com/wiki/21/how-to-work-with-flash-messages/

Thanks for the reply but I have already covered both points in my original post if you care to read it again :)

Can you please post the code that is supposed to print out the flash messages?

Have tried the static code from the URL above:


<?php echo Yii::app()->user->getFlash('success'); ?>

And in layouts -> main.php also similar to what is shown in the URL above:




            <?php

            foreach(array('error', 'notice', 'success') as $key):

                if(Yii::app()->user->hasFlash($key)):

	            echo '<div class="flash-' . $key . '">' . Yii::app()->user->getFlash($key) . '</div>';

                endif;

            endforeach;

       ?>



Thanks.

I have (maybe) the same problem.

All my flash messages work perfectly when a user is logged in (through the WebUser), I see data stored in the session database table (the flash message) and it is removed after reading the flash the next request. All is good.

But, when a user is not logged in, it is not possible to set or get a Flash message. A session is created for non-loggedin users (I see a session in the database table) but the ‘data’ column is not updated with session information (e.g. Flash messages) so also on the next refresh no Flash messages can be restored from the database.

PS. the same goest when I don’t use the database for sessions but just $_SESSION. I also only works for loggedin-users.

Is this normal behaviour or am I missing something? And is this mayb the same issue as you r3d?

Sorry for kicking this thread :slight_smile: But i was hoping that mayb the topicstarter already found an answer or someone else knows the way to go here :-)?

Hi bob, thanks for the reply.

In my case, users must be logged in to access the system so I guess it’s not exactly the same issue.

I still haven’t found a solution either.

Thanks.

Have you tried to var_dump(Yii::app()->user->getFlashes()) ?

You should IMHO do some debug around your code for flash fetching and output.

On the other hand, are you sure the HTML is not output in a weird place and/or is invisible. You may want to check the resulting HTML source as well…

I have the same problem. This is quite strange. My code:




exec('php -version',&$output, $return_val);// for example

Yii::app()->user->setFlash('success','test success');

$this->redirect($returnUrl);



Sometimes the flash message shows, but it is not always showed.

However if I remove the exec() code, the message shows all the time.

So I guess the problem comes from the codes above setFlash(). How can they affect setFlash?

I googled and found someone in a similar situation:

http://stackoverflow.com/questions/4074012/problems-with-getflash-and-setflash-in-yii

Any clues?