In my yii application, when I go to a particular controller/action without login, it will redirect me to the logon page. But, I don’t want to change the url, how can I achieve this? Many thanks
In my yii application, when I go to a particular controller/action without login, it will redirect me to the logon page. But, I don’t want to change the url, how can I achieve this? Many thanks
add this function in your controller
public function accessRules()
{
return array(
array('allow', // allow all users
'users'=>array('*'),
),
);
}
Thanks for helping.
The rules are there already and it can redirect me to the logon page. However, I want it "forward" to the logon page without changing the url. What is the simplest way to do this?
Hi you can call a beforeController method
for e.g
public function beforeControllerAction($controller, $action)
{
if(parent::beforeControllerAction($controller, $action))
{
// this method is called before any module controller action is performed
// you may place customized code here
return true;
}
else
return false;
}
Hi
use this in condition that you prompt the user to login
$this->forward('login');
Thanks for helping.
Where should I put the “$this->forward(‘login’)”? sorry that I’m new to Yii.
Thanks for helping. It seems right to use beforeControllerAction as it should be run before the redirect. Can you tell me where should I put it? Sorry I’m new to Yii.
Hi you can put the action on module class file
Class QuickDialModule extends CWebModule{
public function init(){
// ... code ...
}
// ... code ... other functions
public function beforeControllerAction(){
// ... do whatever you were doing inside the function ...
}
}
Thanks for helping!
I’ve added the beforeControllerAction(), but it didn’t work for me.
Luckily, I found that preFilter can solve my problem. Thanks guys.
remove accessrule for this action
add on the top of your action this code
if (Yii::app()->user->isGuest) $this->forward('login');