Cakephp 4.x CMS教程认证重定向问题

时间:2021-07-26 10:32:12

标签: php cakephp cakephp-4.x

我刚刚重新创建了 CMS 教程。当未登录并尝试编辑文章时,我被重定向到登录页面。成功在这里。 登录后,我被重定向到错误页面。为什么?重定向回文章出错了。见下文

它重定向到的链接:http://localhost/test_cms/test_cms/articles/edit/first-post

链接它应该重定向到 http://localhost/test_cms/articles/edit/first-post

我根据 CMS 教程处理了每个文件,但似乎无法弄清楚这里出了什么问题。

登录代码如下:

public function login()
{
    $this->Authorization->skipAuthorization();

    $this->request->allowMethod(['get', 'post']);
    $result = $this->Authentication->getResult();
    // regardless of POST or GET, redirect if user is logged in
    if ($result->isValid()) {
        // redirect to /articles after login success
        $redirect = $this->request->getQuery('redirect', [
            'controller' => 'Articles',
            'action' => 'index',
        ]);
        return $this->redirect($redirect);
    }
    // display error if user submitted and authentication failed
    if ($this->request->is('post') && !$result->isValid()) {
        $this->Flash->error(__('Invalid username or password'));
    }
}

错误截图:https://ibb.co/PjX8435

2 个答案:

答案 0 :(得分:0)

看起来您在代码中的某处设置了额外的 test_cms,或者用于未经授权操作的 URL 可能会在您的 test_cms 查询参数中附加额外的 redirect。您可以通过在 $redirect 条件内打印 if 变量来调试它。

此外,您可能还想添加一些屏幕截图、错误消息、堆栈跟踪以及您的问题,以帮助人们理解问题以获得更准确的答案。

答案 1 :(得分:0)

我也遇到了这个问题,并找到了解决方法。在我的 UsersController 登录方法中,在 return $this->redirect($redirect); 之前,我添加了以下代码以去除额外的应用程序名称:

if (!is_array($redirect)) {
   if (strncmp($redirect, '/myapp', 6) === 0) {
     $redirect = substr($redirect, 6);
   }
  }

同样,这是一种解决方法。