CakePHP:不能做简单的登录

2021-12-21 00:00:00 php cakephp

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

这是我的 AppController

App::uses('AuthComponent', 'Controller/Component');类 AppController 扩展控制器 {公共 $components = 数组('DebugKit.Toolbar','会议','认证' =>大批('认证' =>大批('表格' =>大批('字段' =>大批('用户名' =>'电子邮件','密码' =>'森哈'),)),'登录重定向' =>数组('控制器' => '用户','动作' => '家'),'注销重定向' =>数组('控制器' => '用户','动作' => '注销'),'authError' =>'对不起,您无法访问它!','登录错误' =>'登录名或密码错误!','自动重定向' =>错误的));}

我的模型用户

class User 扩展 AppModel{公共 $name ="用户";公共函数 beforeSave($options = array()){if (isset($this->data[$this->alias]['senha'])){$this->data[$this->alias]['senha'] = AuthComponent::password($this->data[$this->alias]['senha']);}返回真;}}

仅此而已,无需验证!所以我的用户控制器

 类 UsersController 扩展了 AppController{公共 $name = "用户";公共函数 beforeFilter() {父::beforeFilter();$this->Auth->allow('adicionar');}公共函数索引(){}公共功能家(){$this->render('/home');}公共函数登录(){if($this->request->is('post')){调试($this->请求->数据);死();if($this->Auth->login()){$this->redirect($this->Auth->redirect());}别的{$this->Session->setFlash(__('邮件或密码错误,再试一次!'));}}}公共函数 adicionar(){如果 ($this->request->is('post')) {$this->User->create();if ($this->User->save($this->request->data)) {$this->Session->setFlash(__('用户已被保存'));$this->redirect(array('action' => 'index'));} 别的 {$this->Session->setFlash(__('用户无法保存.请再试一次.'));}}}}

怎么看,action login正在渲染视图Login,有什么问题吗??

和我的查看登录进行身份验证..

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

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

正在更新....

我怎么说,调试我的项目我可以看到!我的请求数据不为空

但我还是有条件的

调试请求数据

解决方案

//AppController

App::uses('AuthComponent', 'Controller/Component');类 AppController 扩展控制器 {/*** 组件** @var 数组*/公共 $components = 数组('认证' =>大批('认证' =>大批('表格' =>大批('字段' =>大批('用户名' =>'电子邮件','密码' =>'森哈'),)),'登录重定向' =>数组('控制器' => '用户','动作' => '家'),'注销重定向' =>数组('控制器' => '用户','动作' => '注销'),'authError' =>'Por 青睐, Faça o login para acessar a Área Restrita','登录错误' =>请登录 ou Senha incorreto!"'自动重定向' =>错误的);

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

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

//控制器

公共函数login(){if($this->request->is('post')){if($this->Auth->login()){$this->redirect($this->Auth->redirect());}别的{$this->Session->setFlash('您的用户名/密码不正确');}}}

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

App::uses('AppModel', 'Model');/*** 用户模型*/类用户扩展 AppModel {公共函数 beforeSave($options = array()) {if (!empty($this->data['User']['password'])) {$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);}返回真;}}

这里是简单的身份验证登录

i'm starting to cook cake haha, everytime that i submit my form for to make the authentication, nothing happens!

This's my 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
    ));


}

My Model User

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;    
    }

}

just it, without validations! So My UsersControllers

    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.'));
                }
        }
    }

}

how you can see, the action login is rendering the view Login, Is there any problem??

And My View Login for authentication..

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

Where am i my wrong? I cann't authenticate..

Updating....

How I had said, debuggin my project i can see! My request data is no empty

but i fell conditional else

Debug Request Data

解决方案

//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 - You have to set Model name User NOT Users in create form

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

//controller

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

    }

//add this to your User model

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;
            }
}

here is the Simple Auth login

相关文章