CakePHP 3.4中的登录重定向 [英] Login redirecting in cakePHP 3.4

查看:86
本文介绍了CakePHP 3.4中的登录重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用cakephp 3.4登录后重定向到当前页面,但我却是这样

I'm trying to redirect to current page after logged in, using cakephp 3.4 but I'm getting like this


localhost页面无法运作,locahost页面会将您重新导向
次。尝试清除Cookie

localhost page isn't working, locahost page redirecting you too many times. Try clearing your cookies

2秒钟,然后将其重定向到主页。请帮我在这里。
这是我的代码

for 2 sec after that it's redirecting to home page. Please help me out here. Here my code

在appController.php

In 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中

In 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.'));
    }
  }
}


推荐答案

好像您在这里有了一些重定向循环。您应该使用 AuthComponent :: redirectUrl()

Looks like you got some redirect loop here. You should use 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来设置用户
登录后应重定向到的目标。

After logging a user in, you’ll generally want to redirect them back to where they came from. Pass a URL in to set the destination a user should be redirected to after logging in.

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

If no parameter is passed, the returned URL will use the following rules:


  • 如果存在
    并且针对相同的URL,则从重定向查询字符串值返回归一化的URL当前应用运行所在的域。
    在3.4.0之前,使用了 Auth.redirect 会话值。

  • 如果没有查询字符串/会话值,并且有配置 loginRedirect ,则 loginRedirect 值。

  • 如果没有重定向值且没有 loginRedirect ,则返回 /

  • Returns the normalized URL from the redirect query string value if it is present and for the same domain the current app is running on. Before 3.4.0, the Auth.redirect session value was used.
  • If there is no query string/session value and there is a config loginRedirect, the loginRedirect value is returned.
  • If there is no redirect value and no loginRedirect, / is returned.

这篇关于CakePHP 3.4中的登录重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆