Dear Friends
The method SiteController::actionLogin calls CWebUser::getReturnUrl method for redirecting the user after successfully logged in.
By going through the code, it is not surprising that if you are not passing a parameter, it is going set the scriptUrl as returnUrl.
CWebUser::getReturnUrl
public function getReturnUrl($defaultUrl=null)
{
return $this->getState('__returnUrl', $defaultUrl===null ? Yii::app()->getRequest()->getScriptUrl() : CHtml::normalizeUrl($defaultUrl));
}
So If you want to redirect the user to somewhere else (for example site/contact),you can do it in the following way in SiteController::actionLogin method.
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->getReturnUrl(array('site/contact')));
}
The setter method stores the returnUrl in session.
CWebUser::setReturnUrl
public function setReturnUrl($value)
{
$this->setState('__returnUrl',$value);
}
When we are setting the some other url as returnUrl in main configuration file,the returnUrl still remains the scriptUrl. So it is obvious that returnUrl is not being set, when application sets the user component.
When we are adding the returnUrl as an property in CWebUser, it is being set.
or
When we are modifying the CWebUser::setReturnUrl method to avoid storing the the value in session then it is being set.
or
When I am setting it in a behavior method attached to ‘onBeginRequest’ event
of application, it works very well.
All these things compound my fear that uninitialised status of one component affects the configuration of other
component if it depends on it , when application is configured by settings in main.php .
[color="#8B0000"]Somebody please clarify on this issue.[/color]
Anyway
I modified the controller.php in components folder in the following way.
Now if user clicks login from one page, after successful logging he is the redirected to that particular page.
It is not going to affect the CWebUser::loginRequired.
In order to achieve this, I have consistently storing the last two requests in session.
I DO NOT KNOW whether it is good practice.
CHttpRequest::getUrlReferrer (to catch the ‘HTTP_REFERER’) is always bringing null values in my localhost.
I have also learnt that it is not consistent across browsers. It can also easily inactivated by client side.
Controller.php in Components Folder
class Controller extends CController
{
public function init()
{
$app=Yii::app();
$url=$app->request->url;;
if($app->session->contains('pastUrl'))
$app->user->returnUrl=$app->session->get('pastUrl');
if($app->session->contains('currentUrl'))
$app->session->add('pastUrl',$app->session->get('currentUrl'));
$app->session->add('currentUrl',$url);
}
Regards.