CakePHP Auth->login() 函数不起作用


CakePHP Auth->login() function not working

这个函数有问题。目前,我已经设置了我的网站,以便只有在用户登录后才能访问"正在打开"和"优惠"页面。如果他们单击这些页面,则会被重定向到登录页面。

不幸的是,当用户登录时,登录页面只是刷新,而不是登录并重定向到索引页面。

"我的用户"表包含以下字段:user_id、名字、姓氏、电子邮件、密码、创建、修改。

这是我在用户控制器中的登录函数的代码:

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        }
        $this->Session->setFlash(__('Invalid username or password, try again'));
    }

这是我的应用程序控制器代码:

App::uses('Controller', 'Controller');
class AppController extends Controller {

        public $helpers = array(
                'Session',
                'Html' => array('className' => 'BoostCake.BoostCakeHtml'),
                'Form' => array('className' => 'BoostCake.BoostCakeForm'),
                'Paginator' => array('className' => 'BoostCake.BoostCakePaginator'),
        );
    // CakePHP documentation tutorial
    public $components = array(
        'Session',
        'DebugKit.Toolbar',
        'Auth' => array(
            'loginRedirect' => array(
                'controller' => 'pages',
                'action' => 'index'
            ),
            'logoutRedirect' => array(
                'controller' => 'pages',
                'action' => 'display',
                'home'
            ),
            'flash' => array(
                'element' => 'alert',
                'key' => 'auth',
                'params' => array(
                    'plugin' => 'BoostCake',
                    'class' => 'alert-error'
                )
            )
        )
    );
    public function beforeFilter() {
        $this->Auth->allow('index', 'contact');
    }
}

这是我的登录代码.ctp:

<!-- Sign in Form which is on the left hand side of the screen -->
        <div class="col-xs-6">
            <?php echo $this->Form->create('User', array(
            'inputDefaults' => array(
                'action' => 'login',
                'div' => 'form-group',
                'label' => array(
                    'class' => 'col col-md-3 control-label'
                    ),
                'wrapInput' => 'col col-md-9',
                'class' => 'form-control'
                ),
                'class' => 'well form-horizontal'
            )); ?>
            <?php echo $this->Form->input('email', array(
                'placeholder' => 'Email'
            )); ?>
            <?php echo $this->Form->input('password', array(
                'placeholder' => 'Password'
            )); ?>
            <!-- Sign in button for the sign in form -->
            <div class="form-group">
                <?php echo $this->Form->submit('Sign in', array(
                    'div' => 'col col-md-9 col-md-offset-3',
                    'class' => 'btn btn-default'
                ));
                echo $this->Form->end(); ?>
            </div>
            <div class="row">
            <h5 small><?php echo $this->Html->link("Not registed? Click here to register!", '/users/register') ?></h5>
            </div>

希望有人能指出出了什么问题

没有将数据发布到控制器和操作。 在创建时尝试此操作:

<?php echo $this->Form->create('User',
                        array('url' => array('controller' => 'users', 'action' => 'login'),
                        'inputDefaults' => array(
                            'div' => 'form-group',
                            'label' => array(
                                'class' => 'col col-md-3 control-label'
                                ),
                            'wrapInput' => 'col col-md-9',
                            'class' => 'form-control'
                            ),
                            'class' => 'well form-horizontal'
                        )); ?>

创建一个 CackPHP 登录按钮

echo $this->Form->end(__('Login'));

您可能会发现此用途已满蛋糕PHP:不能做简单的登录

你用的是什么版本?尝试在此部分使用 redirectUrl 而不是 redirect(),因为您应该在 2.3 版之后使用它:

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

如果它没有帮助,请尝试放置一些回显,看看$this->Auth->login()是否为真以及$this->Auth->redirect()的值是多少。如果一切看起来正常,请尝试查看您的视图是否真的输出了 flash 消息(以防 auth->login 为 false)。

代码看起来很好,因此您确实必须调试一些步骤才能实际找到解决方案。

这将对您有所帮助。请在此处阅读更多内容 配置身份验证处理程序

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

这是因为您忘记了要在组件设置中添加的内容 - 您的公共$components必须看起来像这样-

public $components = array(
    'Session',
    'DebugKit.Toolbar',
    'Auth' => array(
        'loginRedirect' => array(
            'controller' => 'pages',
            'action' => 'index'
        ),
        'logoutRedirect' => array(
            'controller' => 'pages',
            'action' => 'display',
            'home'
        ),
        'authenticate' => array(
            'Form' => array(
                'fields' => array(
                    'username' => 'email'
                )
            )
        ),
        'flash' => array(
            'element' => 'alert',
            'key' => 'auth',
            'params' => array(
                'plugin' => 'BoostCake',
                'class' => 'alert-error'
            )
        )
    )
);