Cakephp Auth->login()对于带有admin前缀的登录操作不起作用


Cakephp Auth->login() not working for login action with admin prefix

大家好,我已经尝试了基本的认证登录,它的工作很好,但当使用admin前缀

 $this->Auth->login() is returning false. 

这里是我的代码片段,

在模型中我添加了

public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }
        return true;
    }

在AppController中,我添加了以下代码

public $components = array(
            'Session',     
            'Auth' => array(
                'loginRedirect' => array('controller' => 'users', 'action' => 'index', 'admin' => true),
                'logoutRedirect' => array('controller' => 'users', 'action' => 'login', 'admin' => true)
            )
        );

在UsersController中我的admin登录如下,

public function admin_login() {
        if ($this->request->is('post')) {
            //$this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']);
            debug($this->request->data['User']['password']);
            debug($this->Auth->login());
            if ($this->Auth->login()) {
                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash(__('Invalid username or password, try again'));
            }
        }
    }

在上面,

debug($this->request->data['User']['password'])返回正确的密码,但是
debug($this->Auth->login())返回false。

我不知道它返回false的原因是什么,是否需要对admin路由进行任何更改。

In routes.php你可以这样写代码:

Router::connect('/admin', array('admin' => true, 'controller' => 'users', 'action' => 'login'));
public $components = array(
      'Session',     
      'Auth' => array(
            'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'login')
      )
);

试试这个。