Redirecting the user to the previous page after authorization (yii2)

I have a main controller from which 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());
              }
           }
        }
        ...
    });
}

It works great for all pages except the captcha page. All captcha pages are redirected to something like this - / captcha /? V = xxxxxxxxxxxxxx

If the object is registered Yii :: $ app-> getRequest (), then I see that for pages with captcha it is used twice. 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 it possible not to track the captcha request?

+4
source share
2 answers

By default, the (generated) controller uses something like this:

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

- ?

, "captcha", captchas ( ). , , , . , - , captcha.

, .

$controller->goBack(). , returnUrl .

: yii\web\Controller

+1

Control Control Filter (ACF) .

use yii\web\Controller;
use yii\filters\AccessControl;

class SiteController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['login', 'logout', 'signup'],
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => ['login', 'signup'],
                        'roles' => ['?'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['logout'],
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }
    // ...
}
0

Source: https://habr.com/ru/post/1543696/


All Articles