我对cakePHP相当陌生,并且阅读了蛋糕网站上的所有教程。我正在使用Cake 2.1构建一个小示例应用程序,并且遇到了问题。基本上,我希望管理员用户在登录时重定向到不同的页面,而不是普通用户重定向到的页面。我确信有一个简单的方法可以做到这一点,但我很挣扎!cakephp auth-> loginRedirect管理员
我有管理路由启用,并使用auth组件(和ACL组件,虽然这不影响我的问题)。我有2个登录管理员 - admin_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');
}
}
因此,大家可以看到默认的是在登录重定向到控制器中添加职位()函数,其中用户工作正常。但是,如何为我的admin_login()函数设置不同的登录重定向?
我读了一些关于这里的职位,但大多数涉及到旧版本的蛋糕,而不是蛋糕2
如果有帮助的,这是在UsersController.php
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.');
}
}
我admin_login功能
任何人都可以指向正确的方向吗?
谢谢,我有类似的东西(user_group),所以我使用过 - 但是我已经在管理员登录功能中完成了它,如果您建议,在beforeFilter中执行它会更好吗? 它应该在过滤器或UsersController beforeFilter之前进入AppController吗? - )感谢您的帮助 – 2012-07-24 10:38:56
把它放在AppController的beforeFilter()和UsersController中的beforeFilter()放入: ** public function beforeFilter(){ parent :: beforeFilter(); //其他代码 } ** – 2012-07-24 10:41:46
如果您没有成功,您可以将我加入MSN,Skype或Facebook,我会查看您的代码。 :) – 2012-07-24 10:49:05