Yii Framework Forum: Flash messages and redirect - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Flash messages and redirect Rate Topic: -----

#1 User is offline   Mursi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 29-November 10

Posted 30 June 2011 - 10:05 AM

Hi,

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 :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..

cheers,
mursi
0

#2 User is online   waitforit 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 390
  • Joined: 09-February 11

Posted 30 June 2011 - 01:41 PM

Are you doing something in your views to display the message?

http://www.yiiframework.com/wiki/21/

Excerpt:

<?php if(Yii::app()->user->hasFlash('success')):?>
    <div class="info">
        <?php echo Yii::app()->user->getFlash('success'); ?>
    </div>
<?php endif; ?>

0

#3 User is offline   Mursi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 29-November 10

Posted 01 July 2011 - 01:45 AM

View Postdniznick, on 30 June 2011 - 01:41 PM, said:

Are you doing something in your views to display the message?

http://www.yiiframework.com/wiki/21/

Excerpt:

<?php if(Yii::app()->user->hasFlash('success')):?>
    <div class="info">
        <?php echo Yii::app()->user->getFlash('success'); ?>
    </div>
<?php endif; ?>


yes ofcourse, thats elementary. however if statement fails with hasFlash ( returns false )
0

#4 User is offline   dmaus 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 22-November 10

Posted 01 July 2011 - 02:27 AM

View PostMursi, on 01 July 2011 - 01:45 AM, said:

yes ofcourse, thats elementary. however if statement fails with hasFlash ( returns false )


Is the session stored in a cookie or appended as HTTP GET parameter?

If the latter, the session id won't be passed by redirect.

Cf. http://php.net/manua...tion.header.php

Quote

Session ID is not passed with Location header even if session.use_trans_sid is enabled. It must by passed manually using SID constant.


vs.

class CHttpRequest extends CApplicationComponent
{
        ...
	public function redirect($url,$terminate=true,$statusCode=302)
	{
		if(strpos($url,'/')===0)
			$url=$this->getHostInfo().$url;
		header('Location: '.$url, true, $statusCode);
		if($terminate)
			Yii::app()->end();
	}
        ...
}

0

#5 User is offline   Mursi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 29-November 10

Posted 01 July 2011 - 04:59 AM

I am not sure, but i think cookies, i did javascript:alert(document.cookie); and that gave me PHPSESSID=[somethign,something..]

Any other ideas??
0

#6 User is offline   Mursi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 29-November 10

Posted 06 July 2011 - 04:25 AM

common, anyone there?? i really need some help here
0

#7 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 06 July 2011 - 04:34 AM

In the layout try an

echo Yii::app()->user->setFlash('notification');


If it shows the message, means that flash message was correctly setted, check why the display is not working.

If you have not the message, check if the code

 Yii::app()->user->setFlash('notification','Your have been successfully logged in by facebook!');


Has been executed.
0

#8 User is offline   Keith Dechant 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 25-April 11

Posted 07 July 2011 - 01:25 PM

*
POPULAR

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
),

9

#9 User is offline   Harman 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 04-June 11
  • Location:Perth

Posted 15 July 2011 - 11:11 AM

View PostKeith Dechant, on 07 July 2011 - 01:25 PM, said:

'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?
Harman Dhillon

Freelance Developer
Perth, Western Australia

0

#10 User is offline   M-J 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 16-August 11

Posted 21 August 2011 - 05:52 PM

This appears to be fixed in v1.1.8.
0

#11 User is offline   lie2815 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 04-November 09

Posted 28 September 2011 - 10:00 AM

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?
0

#12 User is offline   manuel-84 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 91
  • Joined: 06-December 11
  • Location:Sardinia

Posted 16 December 2011 - 01:53 PM

View PostKeith Dechant, on 07 July 2011 - 01:25 PM, said:

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 it worked for me in 1.1.8 :rolleyes:

View PostM-J, on 21 August 2011 - 05:52 PM, said:

This appears to be fixed in v1.1.8.


1.1.8 have the same "problem"
0

#13 User is offline   fleuryc 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 09-November 11

Posted 04 April 2012 - 08:49 AM

Quote

'autoUpdateFlash' => false, // add this line to disable the flash counter


Still not fixed in 1.1.10 ...

Thanks
0

#14 User is offline   maslenkov 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 03-August 12

Posted 03 August 2012 - 07:53 AM

View PostKeith Dechant, on 07 July 2011 - 01:25 PM, said:

'user'=>array(
	'allowAutoLogin'=>false,
	'class'=>'CWebUser',
	'autoUpdateFlash' => false, // add this line to disable the flash counter
),



Keith Dechant thanks!
0

#15 User is offline   Jose H. Milán 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 21-July 11
  • Location:España

Posted 08 March 2013 - 12:51 PM

Thank you,

It works for me in 1.1.13

View PostKeith Dechant, on 07 July 2011 - 01:25 PM, said:

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
),


0

#16 User is offline   Jean Crosignani 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 27-November 13

Posted 31 January 2014 - 04:02 PM

I am using Yii 1.1.13 and have the same problem. But this solution didn't fix it for me :(

I am using Yii-user module for login, and after login I set the flash. And after the redirect, the flash message no longer exists.
0

#17 User is offline   vivek93 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 07-February 14

Posted 18 February 2014 - 04:05 AM

use this code hope it will work for you beacause its working ok for me...

<?php if(Yii::app()->user->hasFlash('success')):?>
<div class="info">
<?php echo Yii::app()->user->getFlash('success'); ?>
</div>
<?php endif; ?>
0

#18 User is offline   vivek93 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 07-February 14

Posted 18 February 2014 - 04:06 AM

View PostJean Crosignani, on 31 January 2014 - 04:02 PM, said:

I am using Yii 1.1.13 and have the same problem. But this solution didn't fix it for me :(

I am using Yii-user module for login, and after login I set the flash. And after the redirect, the flash message no longer exists.

you share your code here will try to help you beacuse its working for me well
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users