我对 cakePHP 相当陌生,并且已经阅读了蛋糕网站上的所有教程。我正在使用 cake 2.1 构建一个小型示例应用程序,但遇到了问题。基本上,我希望管理员用户在登录时重定向到与普通用户重定向到的页面不同的页面。我确信有一个简单的方法可以做到这一点,但我很挣扎!
我启用了管理路由并正在使用身份验证组件(以及 ACL 组件,尽管这不会影响我的问题)。我有 2 个登录名,一个用于管理员 - admin_login() ,另一个用于普通用户登录 - login() - 正常的非管理员登录工作正常。
在我的 AppController.php 中我有这个:
class AppController extends Controller {
public $components = array(
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
)
),
'Session'
);
public $helpers = array('Html', 'Form', 'Session');
public function beforeFilter() {
//Configure AuthComponent
$this->Auth->allow('display'); // Allows access to the homepage of the app
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'posts', 'action' => 'add');
}
}
正如您所看到的,默认情况下用户在登录时会被重定向到帖子控制器的 add() 函数,效果很好。但是,如何为 admin_login() 函数设置不同的登录重定向?
我在这里读过很多帖子,但大多数都与旧版本的 cake 有关,而不是与 cake 2 有关。
如果有帮助,这是我在 UsersController.php 中的 admin_login 函数
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('logout');
}
public function admin_login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
if ($this->Session->read('Auth.User')) {
$this->Session->setFlash('You are logged in!');
//$this->redirect($this->Auth->redirect());
// This doesnt work! Grrrrrr
//$this->redirect(array('controller'=>'pages', 'action'=>'admin_index'));
// This nearly works...
if($this->Auth->user())$this->redirect(array('controller' => 'pages', 'action' => 'admin_index'));
}
}
else {
$this->Session->setFlash('Your username or password was incorrect.');
}
}
有人能指出我正确的方向吗?