setFlash/getFlash problem

hi all,

I am having an issue displaying a flash message. I have the following peculiar situation:

My action has the following code:


Yii::app()->user->logout();

Yii::app()->user->setFlash('index','My custom message here');

$this->redirect(array('site/index'));

In my index file I have:


<?php if(Yii::app()->user->hasFlash('index')): ?>


<div class="success">

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

</div>


<?php endif; ?>

The message just does not appear at all.

I am suspecting that the logout() method might be inverfiering but am not sure. I still hope there may be something else that I’ve missed.

Please advise.

Cheers,

b

So does it work when you remove the logout call?

Hi Y!!,

Yes it does work when logout() method is removed. Any idea on how to implement it to work with the logout() method? Is this the expected behaviour? I am not sure on what technology is being used behind the flash messages but if it uses sessions, which most likely it does, than possibility to achieve it with flashes is zero…

Yes it uses session. I don’t know easy solution. logout(false) doesn’t work I guess since the flash message is set internally via setState(). Also I don’t understand why there is no new session created when calling setFlash() after logout(). :huh:

// As a workaround try defining another WebUser in config. Then use that for flashMessages only (eg Yii::app()->dummyUser->setFlash). Maybe that works.

After talking with bettor via private message, it’s clear now that logout(false) works. The reason is that logout() destroys whole session, so it’s not possible to set anything to the session afterwards. With logout(false), only user relevant data get’s destroyed, not the session itself. This should work as well I guess:




Yii::app()->user->logout();

Yii::app()->session->open();

Yii::app()->user->setFlash(...);



Just want to let you now that this works. If you need to destroy a whole session but you want to set a flash afterwards, you may extends CWebUser this way:


<?php


class BaseWebUser extends CWebUser

{


	public function logout($destroySession = true)

	{

		parent::logout($destroySession);

		Yii::app()->session->open();

	}

	

}


?>