Cakephp 3 未授权重定向不工作

Cakephp 3 unauthorizedRedirect not working

我正在做一个页面,试图为 AppController 中的 auth 组件设置 unauthorizedRedirect 但没有工作,它什么也没做。

我试过把它设置为 false 但没有任何效果

这是应用控制器

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

    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'loginRedirect' => [
            'controller' => 'Pages',
            'action' => 'display'
        ],
        'authError' => 'Seems like you have to use some kind of magic word.',
        'logoutRedirect' => [
            'controller' => 'Pages',
            'action' => 'display',
            'home'
        ],
        'unauthorizedRedirect' => [
            'controller' => 'Users',
            'action' => 'unauthorized'
        ],
    ]);

    //use model companies in all controllers
    $tableCategories = $this->loadModel('Categories');

    $categories = $tableCategories->find()
        ->contain([]);

    $this->set(compact('categories'));
}

public function beforeFilter(Event $event)
{
    $this->set('current_user', $this->Auth->user());
}

}

这是用户控制器

class UsersController extends AppController

{ var $面包屑 = 'Usuarios';

public function beforeFilter(Event $event)
{
    parent::beforeFilter($event);
    $this->Auth->allow(['login', 'unauthorized']);
}

public function login()
{
    $this->viewBuilder()->layout('login');
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect(['controller' => 'pages', 'action' => 'display']);
        }
        $this->Flash->error(__('Invalid username or password, try again'));
    }
}

public function logout()
{
    return $this->redirect($this->Auth->logout());
}

public function unauthorized()
{
    var_dump();
    $this->autoRender = false;

    $message = false;

    echo json_encode($message);exit;
}

它只重定向到登录页面

来自Docs

unauthorizedRedirect Controls handling of unauthorized access. By default unauthorized user is redirected to the referrer URL or loginAction or ‘/’. If set to false, a ForbiddenException exception is thrown instead of redirecting.

unauthorizedRedirect 选项仅适用于经过身份验证的用户。如果经过身份验证的用户试图访问他们无权访问的 URL,他们将被重定向回引荐来源网址。通过指定 unauthorizedRedirect,您现在将用户重定向到指定的 URL 而不是引荐来源网址。

如果您想在错误的登录尝试中重定向用户,则必须在登录方法中手动执行此操作。

希望消除任何疑问。