无法使用cakephp登录

时间:2013-04-18 03:56:09

标签: cakephp authentication

我无法在cakephp中使用Auth登录。在stackoverflow上有一些类似的帖子,但这些答案似乎对我不起作用。

我创建了一个类似的注册表单,它与Auth一起使用但登录后, $这 - > Auth->登录();在UsersController中返回false。

Auth使用正确的userModel,用户名字段更改为电子邮件。 (注意:当我在任何地方使用用户名而不是电子邮件时,它也不起作用)。数据库有一个表用户,其中包含电子邮件和密码字段。使用以下内容对密码字段进行散列/加密:AuthComponent :: password()

// AppController

class AppController extends Controller {

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

    public function beforeFilter() {
        $this->Auth->userModel = 'User';
        $this->Auth->fields = array('username' => 'email', 'password' => 'password');
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'index');
    }
}

// UsersController

class UsersController extends AppController{
  var $name = 'Users';
  var $helpers = array('Html', 'Form');

  function beforeFilter()
  {       
    parent::beforeFilter();
    // tell Auth not to check authentication when doing the 'register' action
    $this->Auth->allow('register');
  }

  function login(){
      if (isset($this->data['User'])) {
        $this->Auth->login();
        debug('tried');
      }
  }

// login.ctp

<?php
    echo $this->Form->create('User',array('action'=>'login'));
    echo $this->Form->input('email');
    echo $this->Form->input('password');
    echo $this->Form->end('Login');  

?>

// user.php

<?php
class User extends AppModel{

    var $name = 'User';
}
?>

我只使用cakephp几天,所以这可能是一个简单的错误,但我现在正在寻找它几个小时,我还没有找到它。

1 个答案:

答案 0 :(得分:1)

$this->Auth->userModel$this->Auth->fields这样的设置属性在2.x中对您没有任何帮助。您需要使用$this->Auth->authenticate属性来指定userModel和fields选项。有关更多信息,请阅读有关Auth配置的2.x manual

相关问题