Shaani
(Aziz Zee)
October 3, 2012, 8:46am
1
In search page I setup a session variable when the page loads. If the user clicks on a save link without being logged in he is redirected to the login page. After successful login he is to be redirected to the same search page. But if the user loads the search page, the session variable is set. If he then goes to index page then goes to login page I don’t want to redirect to search page.
Here is what I have implemented in login action
if(Yii::app()->getController()->getAction()->id=="search"){
if(isset($_SESSION["Search"]) && $_SESSION["Search"]!= null){
$this->redirect(array($_SESSION["Search"]));
}
}
else{
unset(Yii::app()->session['Search']);
}
This is the session variable that is set when the search page loads
$searchurl = end(split('/', $_SERVER['REQUEST_URI']));
Yii::app()->session['Search'] = $searchurl;
Keith
(Kburton)
October 3, 2012, 9:03am
2
if(Yii::app()->getController()->getAction()->id=="search"){...}
What is this doing? Surely if this is implemented in the login action, the action ID is always going to be "login"?
traprajith
(Traprajith)
October 3, 2012, 11:01am
4
Yii::app()->user->returnUrl
Shaani
(Aziz Zee)
October 3, 2012, 12:06pm
5
Keith:
if(Yii::app()->getController()->getAction()->id=="search"){...}
What is this doing? Surely if this is implemented in the login action, the action ID is always going to be "login"?
This is what I was looking for Yii::app()->user->returnUrl. But how can I check the controller or action id of returnUrl ?
Shaani
(Aziz Zee)
October 3, 2012, 12:06pm
6
Thanks Rajith. How can I check the controller or action id of returnUrl ?
mentel
(Yii)
October 3, 2012, 1:25pm
7
You don’t need it. Just use CController::redirect and you’re set.
Shaani
(Aziz Zee)
October 3, 2012, 2:15pm
8
But on successful login on the login page I want to redirect to search page only if the user has come from the search page whose url is …index.php/site/serach?.. In other cases I want to redirect to a certain page after login, which I have already done.
Keith
(Kburton)
October 3, 2012, 2:22pm
9
I’m not sure if it’s good advice, but you could probably look for the substring “site/search” in the URL and base your decision on that. It doesn’t sound like a clean or robust solution though.
mentel
(Yii)
October 3, 2012, 2:27pm
10
Understood.
In this case, you can use CWebUser::returnUrl only for those special cases (like when the user comes from the search page).
When returnUrl is not set, you redirect the user to the default page after login.
traprajith
(Traprajith)
October 4, 2012, 4:42am
11
set in config/main
defaultController'=>'controller/action',
this will full fill ur second demand…
and for search check if(Yii::app()->user->returnUrl ==‘index.php/site/serach’) then return to search …simple…
Shaani
(Aziz Zee)
October 4, 2012, 6:32am
12
set in config/main
defaultController'=>'controller/action',
this will full fill ur second demand…
and for search check if(Yii::app()->user->returnUrl ==‘index.php/site/serach’) then return to search …simple…
Using
die(Yii::app()->user->returnUrl); if(Yii::app()->user->returnUrl =='index.php/site/serach'){
is giving /Index.php
In config/main rules array when I used ‘site’=>‘controller/action’, die(Yii::app()->user->returnUrl); gave the same output /Index.php
‘defaultController’=>‘controller/action’, also gave the same output
mentel
(Yii)
October 4, 2012, 9:58am
13
Please follow Rajith’s recommendation and set the defaultController.
Then you can just redirect to the returnUrl after login, it will always point to the returnUrl you set before the login or to the defaultController.