See the following code of the
<?php
class DefaultController extends Controller
{
public function actionIndex()
{
$this->render('index');
}
/**
* Displays the login page
*/
public function actionLogin()
{
$model = new LoginForm;
// 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);
}
// display the login form
$this->render('login', array('model' => $model));
}
public function filters()
{
return array(
'accessControl',
);
}
public function accessRules()
{
return array(
array('allow', // allow all users to perform the 'login' action
'actions' => array('login'),
'users' => array('*'),
),
array('allow', // allow the admin user to perform everything
'actions' => array('*'),
'users' => array('@'),
),
array('deny', // deny all users
'users' => array('*'),
),
);
}
}
The problem is, when I go to my module: /?r=admin I am redirected to /?r=site/index my default site controller.
How can this happen?
Edit: (added AdminModule)
<?php
class AdminModule extends CWebModule
{
public function init()
{
// this method is called when the module is being created
// you may place code here to customize the module or the application
// import the module-level models and components
$this->setImport(array(
'admin.models.*',
'admin.components.*',
));
$this->setComponents(array(
'user' => array(
'loginUrl' => Yii::app()->createUrl('admin/default/login'),
)
));
}
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;
}
}
As you see I added loginUrl via setComponents, but that does also not work.
If you don’t have a logged in user the access control filter would kick in and redirect you to “home”. The only action you’re allowed to perform on the controller is “login”, so index is part of the denied actions.