Yii Framework Forum: Returnurl Always Points To Index.php - Yii Framework Forum

Jump to content

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

Returnurl Always Points To Index.php Rate Topic: -----

#1 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,197
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 13 October 2012 - 02:51 PM

I have exactly the same problem as GSTAR had, but in my case Yii::app()->user->returnUrl is always pointing to index.php in my application.

And thus, login always redirects me to home page, not there, where I was.

I printed it out to myself in main.php layout file, so I see it on very page. No matter, where I go, it is always index.php, not route to the previously visited page (as supposed to be).

Anyone have any idea, what can be wrong?

I have deployed a fresh, auto-generated Yii application. I have changed it strongly ever since, but I did not mess up anything around redirects, referrers, login etc.

Cookies for sure works, because I can mark "remember me" in login page and don't have to login again, after restarting browser.
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#2 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,168
  • Joined: 05-January 12
  • Location:Morocco

Posted 13 October 2012 - 03:46 PM

I don't know what the problem may be. I have tried returnUrl then ended up using urlReferrer. Don't know if it's flawless.
0

#3 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,168
  • Joined: 05-January 12
  • Location:Morocco

Posted 13 October 2012 - 03:54 PM

See also this thread. Interesting ideas!

http://www.yiiframew...e-previous-url/
0

#4 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,197
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 13 October 2012 - 05:10 PM

The topic, you cited is over two years old and shows way to solve the problem that is already solved in newest versions of Yii -- currently CWebUser is setting returnUrl and nothing of what is shown in that old topic should be done manually.

I started the topic to look for any ideas, why my CWebUser is not setting this variable properly.
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#5 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 14 October 2012 - 11:37 AM

Dear Friend

This is something to share with you on this topic.


CWebUser has no property declared as returnUrl.

When we are going to set this property in main configuration file,it is going to invoke the
method CWebUser::setReturnUrl.

public function setReturnUrl($value)
	{   
		  $this->setState('__returnUrl',$value);
	}

This method instead of assigning the value to returnUrl, it is storing the value in session.

My fear :

When application configures the components by reading the main configuration file, Does the uninitialised state of other components or a component in question affect the creation of that particular component?.In our case when the component user is crearted, is the component session available to configure the user?.



situation 1; This works...

main.php
'components'=>array(
		'user'=>array(
		    'class'=>'application.components.webUser',
		    'allowAutoLogin'=>true,
		     'returnUrl'=>'index.php?r=site/contact',
		),


webUser.php in components folder.
<?php
class webUser extends CWebUser
{
	public $returnUrl;
}	


situation 2: This works... without the property returnUrl

'components'=>array(
		'user'=>array(
		    'class'=>'application.components.webUser',
		    'allowAutoLogin'=>true,
		     'returnUrl'=>'index.php?r=site/contact',
		),


webUser.php in components folder.
class webUser extends CWebUser
{
	//public $returnUrl;	
	public function getReturnUrl($defaultUrl=null)
		{
			return $this->returnUrl;
		}

       public function setReturnUrl($url)
		{
			$this->returnUrl=$url;
		}
}
  
	


situation 3: It works...

Instead of declaring the defaultUrl in main.php, if we declare it down the lane, it works.
Here I am declaring it in a behavior attached to main application without extending the CWebUser.

Main.php
..............................
'behaviors'=>array(
'class'=>'application.components.applicationBehavior',
),
..............................
'components'=>array
		'user'=>array(
			'allowAutoLogin'=>true,
		),


applicationBehavior in components folder.
<?php
class applicationBehavior extends CBehavior
{       
      private $_owner;
      public function events()
           {

                return  array(
			       
			       'onBeginRequest'=>'assignReturnUrl',
			        
		        );
           }
	
		
		public function assignReturnUrl()
               {
			$owner=$this->_owner=$this->getOwner();
			$owner->user->returnUrl='index.php/?r=site/contact';
		}
	
}
?>



All these results compound my fear that the problem arises when components are created and initialized.
I need your inputs on this.

As suggested by bennouna, I was trying to get 'HTTP_REFERER' from CHttpRequest::getUrlReferrer to get
returnUrl.It is always bringing null in my localhost in all the browsers.

I tried to catch the the last two requests and put them in session. So when user jumps from one particular page into login page, after successful login he again redireced to that particular page.

applicationBehavior.php in components folder.
<?php
class applicationBehavior extends CBehavior
{       
      private $_owner;
      public function events()
           {

                return  array(
			       
			       'onBeginRequest'=>'assignReturnUrl',
			        
		        );
           }
	
		
		public function assignReturnUrl()
               {
			$owner=$this->_owner=$this->getOwner();

			if($owner->session->contains('pastUrl'))
				 $owner->user->returnUrl=$owner->session->get('pastUrl');
			
			if($owner->session->contains('currentUrl'))
			   $owner->session->add('pastUrl',$owner->session->get('currentUrl'));

			$owner->session->add('currentUrl',$owner->request->url);
		}
	
}
?>



The issue needs further clarification.

Regards.
0

#6 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,197
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 15 October 2012 - 04:17 PM

Hi,

Thank you for your long and detailed answer.

View Postseenivasan, on 14 October 2012 - 11:37 AM, said:

CWebUser has no property declared as returnUrl

Hm... What about this?

There is such property like CWebUser.returnUrl, but -- as I discovered -- it is only set properly in situations, where login process is called automatically by Yii. That is: if user is not logged-in (authenticated) and requests any operation that requires authentication. In any other situation, returnUrl contains "index.php", and this is considered as Yii feature, not a bug (at least now).

This is kind of big problem. You not only have to record your current URL, but you also have to take care of not recording login form URL or actions like CCaptchaActions route. In other word, this is (as for me) to big task, for time resources I have to spend on this. So, unless this will become fixed in next release of Yii, I'm leaving it like it is now -- i.e. after manual login, user will be redirected to home page.
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#7 User is offline   gaz 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 03-January 13

Posted 16 January 2013 - 11:05 PM

Hi all

I am experiencing a similar issue. I am new to Yii and have read some of the posts and have managed to get some aspects working, such as after successful login, going to the correct page and not the index page as before.

However, when I enter this web address 'http://trackstar:8888/index.php?r=project' in the browser I am not redirected to the Login screen as I should because I haven't logged in yet.

I am working from Jeffrey Winesett's latest book (2012) on Yii and he states that after running the CRUD Generator, and without any configuration of the application it should work: ie
#1 Successfully log in and you should go to the 'project' section of the app.
#2 enter the above address and you should be redirected to the Login screen.

Anyone wanting to know what I have done to fix the first part see below. As far as the second issue is concerned I am at a loss.


(from SiteController)

	public function actionLogin()
	{
		$model=new LoginForm;
		$yourUrl='index.php?r=project/create';   //add this
		

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				//$this->redirect(Yii::app()->user->returnUrl);  // Orignal code

				$this->redirect(Yii::app()->user->getReturnUrl($yourUrl)); // and Add this!!!

		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}

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