I am considerably new to Yii, and the whole MVC architecture, for that matter, most computer architectures. Anyway, i have figured out most of the details, i am right now having a problem, and i have searched the forum thoroughly and didnt find any solutions.
In short my problem is after a set a flash message and i redirect the flash message doesn’t appear properly
Here are some code to make my point more clear.
I have a method as such in my controller.
public function actionFacebook(){
/* Facebook graph and authentication process..*/
if($fb_success){
Yii::app()->user->setFlash('notification','Your have been successfully logged in by facebook!');
$this->redirect(array('site/index'));
}
else
die('and go to hell <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/biggrin.gif' class='bbc_emoticon' alt=':D' />');
}
I hope the idea is clear enough, i am looking for a good solution to it, try to be as conventional as possible.
I dont mind work arounds, i tried using Yii::app()->user->session[‘message’] and destroying the session, but with no successs…
The flash messages have a counter, which starts at one. Every time Yii starts a new page request, the counter gets decremented by one. When the counter reaches zero, that flash message gets deleted. If you haven’t displayed it by then, you never can. This is meant to make the flash messages available on the current and next requests, but it doesn’t seem to work the way it was intended.
When you redirect, you are ending the current request and starting a new one. This means the flash counters count down by one and the messages get deleted. This happens before your controller runs. Therefore you can’t display the flash messages after a redirect.
Yii 1.1.7 and later include a property CWebUser::autoUpdateFlash which can be set to false to turn off the flash message counter. Messages will then stay in the session until you display them.
Edit the ‘components’=>‘user’ section of your config file like this:
'user'=>array(
'allowAutoLogin'=>false,
'class'=>'CWebUser',
'autoUpdateFlash' => false, // add this line to disable the flash counter
),
Thanks Keith. That fixed my problem. Though i am still a bit confused.
For me the same problem was coming up, but only in Firefox browser (v4 and v5). I’m using Yii 1.1.8 and used the exact code as the yiiblog demo. While the demo worked fine, my code didnt work until i added the above lines mentioned by you. I wonder why?
I tried this solution, too. Unfortunately, it seems like this solution does not work when logging out - probably because the cookie that stores information like that is being restored.
Is that a bug or can I work around that somehow? How do you guys display flash messages when logging out?