1

我正在尝试使用 cakephp 3.4 登录后重定向到当前页面,但我得到了这样的结果

本地主机页面不工作,本地主机页面重定向您太​​多次。尝试清除 cookie

2 秒后,它重定向到主页。请帮帮我。这是我的代码

在 appController.php

public function initialize()
{ 
    parent::initialize();
    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'authorize' => ['Controller'],
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ],
                'scope' => ['userStatus' => '1']
            ]
        ],
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
        'unauthorizedRedirect' => $this->referer(),
        'logoutRedirect'       => [
                'controller' => 'Users',
                'action'     => 'login'
        ]
    ]);
}

在 loginController.php

function login{ 
 if ( $this->request->is( 'post' ) ) {
    if ( $this->Auth->login() ) 
    {
       $this->redirect($this->referer());
    } 
    else {
      $this->Flash->error(__('Your username or password is incorrect.'));
    }
  }
}
4

2 回答 2

2

看起来你在这里有一些重定向循环。你应该使用AuthComponent::redirectUrl().

public function login()
{
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();

        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Flash->error(__('Username or password is incorrect'));
        }
    }
}

请参阅文档中的登录后重定向用户。

用户登录后,您通常希望将他们重定向回他们的来源。传入 URL 以设置用户在登录后应重定向到的目标。

如果不传递任何参数,则返回的 URL 将使用以下规则:

  • 从重定向查询字符串值返回规范化 URL(如果存在)并且对于当前应用程序正在运行的同一域。在 3.4.0 之前,Auth.redirect使用的是会话值。
  • 如果没有查询字符串/会话值并且有 config loginRedirectloginRedirect则返回该值。
  • 如果没有重定向值并且没有loginRedirect/则返回。
于 2017-05-30T13:07:48.773 回答
0

使用$this->Auth->redirectUrl()而不是$this->referer().

用户登录后,您通常希望将他们重定向回他们的来源。传入 URL 以设置用户在登录后应重定向到的目标。

  • 从重定向查询字符串值返回规范化 URL(如果存在)并且对于当前应用程序正在运行的同一域。在 3.4.0 之前,使用的是 Auth.redirect 会话值。
  • 如果没有查询字符串/会话值并且有配置 loginRedirect,则返回 loginRedirect 值。
  • 如果没有重定向值并且没有loginRedirect/则返回。

添加到您的AuthComponent配置选项:

登录重定向

用户登录后应重定向到控制器操作的 URL(定义为字符串或数组)。如果用户在其会话中具有 Auth.redirect 值,则该值将被忽略。

你的代码应该是这样的:

在 appController.php

public function initialize()
{ 
    parent::initialize();
    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'authorize' => ['Controller'],
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ],
                'scope' => ['userStatus' => '1']
            ]
        ],
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
        'unauthorizedRedirect' => $this->referer(),
        'logoutRedirect'       => [
                'controller' => 'Users',
                'action'     => 'login'
        ],
        'loginRedirect' => [
            'controller' => 'Pages',
            'action' => 'display'     
        ]
    ]);
}

在 loginController.php

function login{ 
 if ( $this->request->is( 'post' ) ) {
    if ( $this->Auth->login() ) 
    {
       $this->redirect($this->Auth->redirectUrl());
    } 
    else {
      $this->Flash->error(__('Your username or password is incorrect.'));
    }
  }
}

另请参阅登录后重定向用户

于 2017-05-30T13:08:34.257 回答