2016-01-19 17 views
0

我使用CakePHP 2.7.8登录代码使用身份验证组件

TABLENAME - 用户表

领域

ID 名 电子邮件 密码

我正在尝试登录,如果登录成功,那么它会重定向到广告页面。

,但我现在不为什么

我的登录显示错误表格后提交

无效的用户名和密码。

但我的表格包含可用的确切值。

plz帮我找出错误。

控制器文件名 - UsersController.php

<?php 
class UsersController extends AppController { 

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

    public function beforeFilter() 
    { 
     parent::beforeFilter();  
     $this->Auth->allow('add'); 
    } 

    public function login() { 
     if ($this->request->is('post')) { 
       //print_r($this->request->is('post')); 
       //exit; 
       if ($this->Auth->login()) { 
        return $this->redirect($this->Auth->redirect(array('controller' => 'Users', 'action' => 'add'))); 
       } 
       $this->Flash->error(__('Invalid username or password, try again')); 
      } 
     } 
} 
?> 

型号文件名 - user.php的

<?php 

class User extends AppModel { 

public $validate = array('lname' => array('rule' => 'notEmpty')); 

} 
?> 

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'); 
?> 

Appcontroller.php

<?php 
App::uses('Controller', 'Controller'); 

class AppController extends Controller { 

    public $components = array(
     'Flash', 
     'Auth' => array(
      'loginRedirect' => array(
       'controller' => 'Users', 
       'action' => 'index' 
      ), 
      'logoutRedirect' => array(
       'controller' => 'Users', 
       'action' => 'display', 
       'home' 
      ) 
     ) 
    ); 

    public function beforeFilter() { 
     $this->Auth->allow('index', 'view','update','delete'); 
    } 


} 
?> 
+0

** http://stackoverflow.com/search?q =%5Bcakephp%5D + auth + email + instead + of + username ** – ndm

回答

0

有没有必要宣布此行

 public $components = array('Flash'); // in UsersController.php 

因为你已经宣布在AppController中本身的组件阵列。 - >>

public $components = array(
    'Flash', 
    'Auth' => array(
     'loginRedirect' => array(
      'controller' => 'Users', 
      'action' => 'index' 
     ), 
     'logoutRedirect' => array(
      'controller' => 'Users', 
      'action' => 'display', 
      'home' 
     ) 
    ) 
); 

此外,您可以修改此登录部分。

if ($this->Auth->login()) { 
     return $this->redirect(array('controller' => 'Users', 'action' => 'index')); 

     or 

     return $this->redirect($this->Auth->redirectUrl()); 
    } 

它有时会导致Session和重定向Url的问题。编写注销代码并尝试重新登录。它有时会起作用。

此外,尝试调试和写:

pr($this->Auth->user()); in your login action in UsersController. 

如果用户登录,它将返回登录用户的一组数据。

和平! xD