CakePHP的身份验证组件不工作 [英] cakePHP auth component not working

查看:308
本文介绍了CakePHP的身份验证组件不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有蛋糕的身份验证的一个问题,我似乎根本无法让过去(我一直在调试和最后两天尝试不同的教程)。至于我可以看到它应该是很简单的,问题是每当我尝试登录,它只是刷新登录页面。我不能为我的生活弄清楚为什么!我唯一​​的结论是,必须有一些(基本)的教程想当然地认为我已经错过了。

I have an issue with cake's auth that I simply can't seem to get past (i've been debugging and trying different tutorials for the last two days). As far as I can see it should be very simple, the problem is whenever i try to login, it just refreshes the login page. I cannot for the life of me figure out why! My only conclusion is that there must be something (basic) which tutorials take for granted that I have missed.

下面是几个片断:

users_controller.php中

users_controller.php

class UsersController extends AppController {

    var $name = 'Users';

    function beforeFiler() {
        parent::beforeFilter();
    }
    function login() {
    }

    function logout() {
        $this->Session->setFlash('You have successfully logged out.');
        $this->redirect($this->Auth->logout());
    }
}

app_controller.php

app_controller.php

class AppController extends Controller {

    var $helpers = array('Html','Form','Javascript');
    var $components = array('Auth');

    function beforeFilter() {

        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'contents', 'action' => 'index');
        $this->Auth->logoutRedirect = array('controller' => 'contents', 'action' => 'view');
        $this->Auth->loginError = 'Something went wrong';
        $this->Auth->allow('register', 'view');
        $this->Auth->authorize = 'controller';
        $this->set('loggedIn', $this->Auth->user('id'));
    }

    function isAuthorized() {
        return true;
    }
}

login.ctp

login.ctp

<div class="midCol short">
    <h3>Login</h3>
    <div class="loginBox">
    <?php e($form->create('User', array('controller'=>'users','action'=>'login')));?>
        <?php
        echo $this->Form->input('username');
        echo $this->Form->input('password');
        e($this->Form->end(array('label'=>'Login', 'class'=>'loginButton button png')));?>
    </div>
</div>

任何帮助将大大AP preciated,这有我撕裂我的头发了!

Any help would be greatly appreciated, this has me tearing my hair out!

推荐答案

只是为了文档,因为我有很难找到适合在网络上CakePHP的2.x的答案。这东西必须是正确的,以便使用表单验证:

Just for documentation as I had difficulties finding an answer for CakePHP 2.x on the web. This stuff needs to be "correct" in order to use Form authentication:


  • 该配置需要是正确的,例如在UsersController(领域配置时的名字在DB不同的是真的只需要):

  • The config needs to be right, e.g. in your UsersController (the fields config is really only required when names differ in the DB):

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


  • 您必须使用表单助手:形式 - >创建增加了一个隐藏的输入字段(后),并通过形式 - 生成的输入字段>输入的名称()遵循一个约定的验证组件需要。

  • You have to use the Form Helper: Form->create adds a hidden input field ("post"), and the names of the input fields generated by Form->input() follow a convention that the Auth component expects.

    用户 - >登录不得通过自定义的数据Auth->登录()。该验证组件会从表单(=请求)在auth数据。

    User->login must not pass custom data to Auth->login(). The Auth component will take the auth data from the form (= request).

    这篇关于CakePHP的身份验证组件不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆