2012-07-24 59 views
0

我对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功能

任何人都可以指向正确的方向吗?

回答

2

如果你有角色替换beforeFilter()你loginRedirect设置 - > “管理,用户” 在你的数据库,把你的AppController - beforeFilter():

if($this->Auth->user('role') == 'admin'){ 
    $this->Auth->loginRedirect = array('controller' => 'controller1', 'action' => 'action1'); 
}else{ 
    $this->Auth->loginRedirect = array('controller' => 'controller2', 'action' => 'action2'); 
} 

如果你想知道什么是在$这个 - > Auth->用户()只放:

debug($this->Auth->user()); 
+0

谢谢,我有类似的东西(user_group),所以我使用过 - 但是我已经在管理员登录功能中完成了它,如果您建议,在beforeFilter中执行它会更好吗? 它应该在过滤器或UsersController beforeFilter之前进入AppController吗? - )感谢您的帮助 – 2012-07-24 10:38:56

+0

把它放在AppController的beforeFilter()和UsersController中的beforeFilter()放入: ** public function beforeFilter(){ parent :: beforeFilter(); //其他代码 } ** – 2012-07-24 10:41:46

+0

如果您没有成功,您可以将我加入MSN,Skype或Facebook,我会查看您的代码。 :) – 2012-07-24 10:49:05

0

也许这个作品:

if($this->request->params['admin']) { 
    $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'index', 'admin' => true); 
} 
else { 
    $this->Auth->loginRedirect = array('controller' => 'posts', 'action' => 'add'); 
} 

与这些线

+0

谢谢,但我试过你的建议,但是当我作为管理员登录它只是离开我的登录页面。我有这样的admin_login()函数中: 公共职能admin_login(){ \t如果($这个 - >请求 - >是( '后')){ \t如果($这个 - > Auth->登录( )){ \t \t如果($这个 - >会话级>阅读( 'Auth.User')){ \t \t \t $这个 - >会话级> setFlash( '您的登录!'); \t \t \t $ this-> redirect($ this-> Auth-> redirect()); – 2012-07-24 09:19:05