身份验证后将用户重定向到上一页(yii2) [英] Redirect user to previous page after auth (yii2)

查看:28
本文介绍了身份验证后将用户重定向到上一页(yii2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有主控制器,其他控制器都继承自该主控制器.代码是这样的

I have the main controller from which the others are inherited. Code is something like this

public function init()
{
    $this->on('beforeAction', function ($event) {
        ...

        if (Yii::$app->getUser()->isGuest) {
            $request = Yii::$app->getRequest();
            // dont remember login page or ajax-request
            if (!($request->getIsAjax() || strpos($request->getUrl(), 'login') !== false))                  {
               Yii::$app->getUser()->setReturnUrl($request->getUrl());
              }
           }
        }
        ...
    });
}

它适用于所有页面,除了带有验证码的页面.所有带有验证码的页面都被重定向到这样的东西 -/captcha/?v=xxxxxxxxxxxxxx

It works perfectly for all pages, except the page with captcha. All the pages with captcha are redirected to something like this - /captcha/?v=xxxxxxxxxxxxxx

如果对象被记录 Yii::$app->getRequest() 那么我看到对于带有验证码的页面它被使用了两次.第一次对象是正确的,第二次我看到带有验证码的对象.我怎样才能用 yii 解决这个问题?有没有机会不跟踪验证码请求?

If the object is logged Yii::$app->getRequest() then I see that for pages with captcha it is used twice. For the first time the object is corect, and the second time I see the object with captcha. How can I solve this problem with yii? Is there a chance not to track the request for captcha?

推荐答案

默认(生成的)控制器使用如下内容:

The default (generated) controller uses something like this:

public function actions()
{
    return [
        'captcha' => [
            'class' => 'yii\captcha\CaptchaAction',
        ],
    ];
}

您的控制器是否包含类似的内容?

Does your controller contain something like this?

这意味着有一个动作验证码"用于显示验证码(它返回图像).当您有一个显示验证码的页面时,会在您要返回的页面之后调用该图像.因此,最近访问的页面是带有验证码的页面.

This means that there is an action "captcha" that is used for displaying captchas (it returns the image). When you have a page displaying a captcha the image is called after the page you want to return to. Therefore that latest page visited is the one with the captcha.

我认为你必须过滤掉这个动作.

I think you have to filter out this action.

另一种可能是使用默认的 $controller->goBack() 方法.我认为这会默认处理 returnUrl 的注册.

Another possibility could be to use the default $controller->goBack() method. I think this handles registering of the returnUrl by default.

参考:类 yii\web\控制器

这篇关于身份验证后将用户重定向到上一页(yii2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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