6

在我看来,我有:

<?php
echo $this->Form->create('User', array("controller" => "Users", "action" => "login", "method" => "post"));
echo $this->Form->input('User.email', array("label" => false));
echo $this->Form->input('User.password', array("label" => false, 'class' => 'password-input'));
echo $this->Form->end(); ?>

在我的 AppController 中:

public $components = array(
        'Session',
        'Auth'
    );

    function beforeFilter(){
        $this->Auth->fields = array(
            'username' => 'email',
            'password' => 'password'
        );
    }

在我的用户控制器中:

function beforeFilter(){
        $this->Auth->allow('sign_up', 'login', 'logout', 'forgot_password');
        return parent::beforeFilter();
    }
public function login() {
        if ($this->Auth->login()) {
            $this->Session->setFlash(__('Successfully logged in'), 'default', array('class' => 'success'));
            $this->redirect($this->Auth->redirect());
        } else {
            if (!empty($this->request->data)) {
                $this->Session->setFlash(__('Username or password is incorrect'), 'default', array('class' => 'notice'));
            }
        }
    }

但是登录不起作用,我错过了什么?

谢谢。

4

2 回答 2

14

我相信问题是:

function beforeFilter(){
    $this->Auth->fields = array(
        'username' => 'email',
        'password' => 'password'
    );
}

这就是在 CakePHP 1.3 中指定自定义登录字段的方式。CakePHP 2.0 要求您在public $components = array(...);. 1.3 API显示Auth 有 $fields 属性,但2.0 API显示不再有 $fields 属性。所以你必须:

public $components = array(
    'Session',
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email')
            )
        )
    )
);

更多信息请访问:http ://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#configuring-authentication-handlers

请告诉我它是如何工作的!

于 2012-04-21T06:06:43.987 回答
0

我的问题的最终解决方案。谢谢你。

我遇到了 userModel 的问题,我写了这个:

'Auth' => array(
         'userModel' => 'Member'
      )

而不是这个:

'Auth' => array(
    'authenticate' => array(
        'Form' => array(
            'userModel' => 'Member'
        )
    )
)
于 2012-08-06T13:24:01.047 回答