如何在cakephp身份验证中删除“重定向”参数?

时间:2019-01-30 16:15:41

标签: authentication cakephp-3.0

我希望将用户重定向到登录页面(如果未登录),但URL中没有“ redirect”参数。

这是我的代码:

<code>
    $this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ]
            ],
        ],
        'loginAction' => [
            'controller' => 'Auth',
            'action' => 'login'
        ],
    ]);
</code>

我希望输出为 admin / auth / login ,但实际输出为 admin / auth / login?redirect =%2Fadmin%2F

我已经查看了文档,却一无所获。

2 个答案:

答案 0 :(得分:0)

我通过重写AuthComponent类(Cake \ Controller \ Component \ AuthComponent)解决了该问题。为此,我在文档之后为组件(https://book.cakephp.org/3.0/en/controllers/components.html#aliasing-components)创建了别名

我重写了 _loginActionRedirectUrl()函数,使其返回 $ this-> _ config ['loginAction'];

答案 1 :(得分:0)

您需要将参数'unauthorizedRedirect' => false添加到代码中。因此您的代码将类似于

$this->loadComponent('Auth', [
    'authenticate' => [
        'Form' => [
            'fields' => [`enter code here`
                'username' => 'email',
                'password' => 'password'
            ]
        ],`enter code here`
    ],
    'loginAction' => [
        'controller' => 'Auth',
        'action' => 'login'
    ],
    'unauthorizedRedirect' => false
]);

希望对您有帮助。
要了解更多信息,请查看Here