CakePHP:不能做简单登录

时间:2014-03-30 18:44:50

标签: php cakephp

我开始做蛋糕哈哈,每次我提交表单进行身份验证时,都没有任何反应!

这是我的AppController

App::uses('AuthComponent', 'Controller/Component');

class AppController extends Controller {

    public $components = array(
            'DebugKit.Toolbar',
            'Session',
            'Auth' => array(
                'authenticate' => array(
                    'Form' => array(
                        'fields' => array(
                            'username' => 'email',
                            'password' => 'senha'
                        ),

                    )
                ),
                'loginRedirect' => array('controller' => 'users', 'action' => 'home'),
                'logoutRedirect' => array('controller' => 'users', 'action' => 'logout'),
                'authError' => 'Sorry, You Can not Access it!',
                'loginError' => 'Login or Password incorrets!',
                'autoRedirect' => false
    ));


}

我的模特用户

class User extends AppModel
{

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

}
只是它,没有验证!所以我的用户控制器

    class UsersController extends AppController
{

    public $name = "Users";

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

    public function index(){

    }

    public function home(){
        $this->render('/home');
    }

    public function login(){    
        if($this->request->is('post')){ 
            debug($this->request->data);
            die();  
            if($this->Auth->login()){
                $this->redirect($this->Auth->redirect());
            }else{
                $this->Session->setFlash(__('Mail or Password incorrets, Try again!'));
            }
        }   
    }

    public function adicionar(){
         if ($this->request->is('post')) {
                $this->User->create();
                if ($this->User->save($this->request->data)) {
                    $this->Session->setFlash(__('The user has been saved'));
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
                }
        }
    }

}

你怎么看,动作登录是渲染视图登录,有什么问题吗?

我的浏览登录验证..

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

我错在哪里?我无法验证..

...更新

我怎么说,调试我能看到的项目!我的请求数据不是空的

enter image description here

但我有条件的其他

enter image description here

调试请求数据

enter image description here

2 个答案:

答案 0 :(得分:2)

// AppController的

App::uses('AuthComponent', 'Controller/Component');

class AppController extends Controller {

    /** 
     * Components
     *
     * @var array
     */

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

                )
            ),
            'loginRedirect' => array('controller' => 'users', 'action' => 'home'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'logout'),
            'authError' => 'Por favor, Faça o login para acessar a Área Restrita',
            'loginError' => 'Login ou Senha incorreto!'
            'autoRedirect' => false
    );

view - 您必须在创建表单中设置模型名称User NOT Users

<?php echo $this->Form->create('User',array('url' => array('controller' => 'users', 'action' => 'login')); 
         echo $this->Form->input('email');
          echo $this->Form->input('senha');
   echo $this->Form->end(); ?> 

//控制器

public function login()
    {

        if($this->request->is('post'))
        {
            if($this->Auth->login())
            {
                $this->redirect($this->Auth->redirect());
            }else
            {
            $this->Session->setFlash('Your username/password was incorrect');
            }
        }

    }

//将此添加到您的用户模型

App::uses('AppModel', 'Model');
/**
 * User Model
 */
class User extends AppModel {        

    public function beforeSave($options = array()) {

                if (!empty($this->data['User']['password'])) {
                    $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
                } 
                return true;
            }
}

这是Simple Auth login

答案 1 :(得分:1)

我认为你的问题在这里

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

试试这个

  public function beforeFilter()
    { 
    $this->Auth->authenticate = array(          
                    'Form' => array(
                        'fields' => array(
                            'username' => 'email',
                            'password' => 'senha'
                                )
                            )
                        )
    }

* @@@ UPDATE @@@@ *

嗯,最后我得到了解决方案......我想是的......

问题在于您的视图。使用此 -

<?php echo $this->Form->create('User',array('action'=>'login')); ?>
    <div class="input">
      <div class="blockinput">
        <i class="icon-envelope-alt"></i>
        <input type="mail" name="email" placeholder="Email">

        <?php echo $this->Form->input('email', array('type' => 'email', 'placeholder' => 'Email', 'label' => false));?>

      </div>
      <div class="blockinput">
        <i class="icon-unlock"></i>

       <?php echo $this->Form->input('senha', array('type' => 'password', 'placeholder' => 'Senha', 'label' => false));?>

      </div>
    </div>
       <button>Entrar</button>
  <?php echo $this->Form->end(); ?>