CakePHP 3.6身份验证不起作用

时间:2018-04-19 18:56:28

标签: php cakephp

我正在关注CakePHP教程:
https://book.cakephp.org/3.0/en/tutorials-and-examples/bookmarks/part-two.html

我的CakePHP版本是:3.6.0
我的Php版本是:7.2

这是我的src \ Controller \ AppController.php

<?php
namespace App\Controller;

use Cake\Controller\Controller;
use Cake\Event\Event;
class AppController extends Controller
{

    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('RequestHandler');
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'authenticate' => [
                'Form' => [
                    'fields' => [
                        'username' => 'email',
                        'password' => 'password'
                    ]
                ]
            ],
            'loginAction' => [
                'controller' => 'Users',
                'action' => 'login'
            ],
            'unauthorizedRedirect' => $this->referer() 
        ]);

        $this->Auth->allow(['display']);

    }
}

在我的src \ Controller \ UsersController.php中,我添加了以下方法:

public function login()
{
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->error('Your username or password is incorrect.');
    }
}

我创建了src \ Template \ Users \ login.ctp文件:

<h1>Login</h1>
<?= $this->Form->create() ?>
<?= $this->Form->control('email') ?>
<?= $this->Form->control('password') ?>
<?= $this->Form->button('Login') ?>
<?= $this->Form->end() ?>

我的浏览器正在通知错误:ERR_TOO_MANY_REDIRECTS

如果我尝试转到以下网址:localhost:8765 / bookmarks

浏览器重定向到:http://localhost:8765/users/login?redirect=%2Fusers%2Flogin%3Fredirect%3D%252Fusers%252Flogin%253Fredirect%253D%25252Fusers%25252Flogin%25253Fredirect%25253D%2525252Fusers%2525252Flogin%2525253Fredirect%2525253D%252525252Fusers%252525252Flogin%252525253Fredirect%252525253D%25252525252Fusers%25252525252Flogin%25252525253Fredirect%25252525253D%2525252525252Fusers%2525252525252Flogin%2525252525253Fredirect%2525252525253D%252525252525252Fusers%252525252525252Flogin%252525252525253Fredirect%252525252525253D%25252525252525252Fusers%25252525252525252Flogin%25252525252525253Fredirect%25252525252525253D%2525252525252525252Fusers%2525252525252525252Flogin%2525252525252525253Fredirect%2525252525252525253D%252525252525252525252Fusers%252525252525252525252Flogin%252525252525252525253Fredirect%252525252525252525253D%25252525252525252525252Fusers%25252525252525252525252Flogin%25252525252525252525253Fredirect%25252525252525252525253D%2525252525252525252525252Fusers%2525252525252525252525252Flogin%2525252525252525252525253Fredirect%2525252525252525252525253D%252525252525252525252525252Fusers%252525252525252525252525252Flogin%252525252525252525252525253Fredirect%252525252525252525252525253D%25252525252525252525252525252Fusers%25252525252525252525252525252Flogin%25252525252525252525252525253Fredirect%25252525252525252525252525253D%2525252525252525252525252525252Fusers%2525252525252525252525252525252Flogin%2525252525252525252525252525253Fredirect%2525252525252525252525252525253D%252525252525252525252525252525252Fusers%252525252525252525252525252525252Flogin%252525252525252525252525252525253Fredirect%252525252525252525252525252525253D%25252525252525252525252525252525252Fusers%25252525252525252525252525252525252Flogin%25252525252525252525252525252525253Fredirect%25252525252525252525252525252525253D%2525252525252525252525252525252525252Fusers%2525252525252525252525252525252525252Flogin%2525252525252525252525252525252525253Fredirect%2525252525252525252525252525252525253D%252525252525252525252525252525252525252Fbookmarks

1 个答案:

答案 0 :(得分:1)

您应该将login操作添加到允许列表中:

$this->Auth->allow([
    'display',
    'login',
]);

或阅读此评论:https://stackoverflow.com/a/49928585/182823

相关问题