goBack ()" I have views from various controller actions that should only be executed from a...">

Yii2: exclude certain controller actions from "$ this-> goBack ()"

I have views from various controller actions that should only be executed from an iframe placed in another view.

Currently, when the iframe loads and I go to the login page, if successful, the login controller (using the user module yii2) calls $this->goBack() , redirecting me to the original iframe URL (since it is the last page visited), not the original page containing the iframe.

Basically, I would like to exclude certain controller actions that will be set as the return URL when $this->goBack() called. Bonus points if all actions loaded in the iframe are automatically excluded from $this->goBack() .

+5
source share
1 answer

Ok, I will go for it! This code is not fully verified! Your problem is that the action has no way of knowing if it was called from the iframe or not if you don't give it. So, the foundation of my attempt to answer is that all the URLs for the iframe must have an additional get parameter. Lets call it caller . Therefore, each iframe should look something like this:

 <iframe url="index.php?r=controller/action&caller=this-controller/action</iframe> 

Now you can always check the request URL to see if it is called from the iframe. In addition, each link inside the iframe must have this parameter added to this URL.

So now we have at least two problems. Firstly, how to automatically add caller as a get parameter without rewriting each URL, and secondly, how to reconfigure the goBack() method so that it knows the difference between the two types of requests.

The first problem can be relatively easily solved by adding another level of representation between the controller and the view you want, I will call it iframe . So in your controller action add this:

 $view = 'The name of the view you want to render'; $this->render('iframe', 'view' => $view);//Add in any other parameters you want to pass 

Your iframe view file should have something like this:

 <iframe src="<?php Url::to(['however you generate the url for your iframe', 'caller' => Url::to($this->context->route)]); ?>"> <?php $this->render($view); ?>//Pass additional parameters to the view if needed </iframe> 

We now have a way to test the controller/action call to see if it is being requested by am iframe. The caller parameter is important because it allows us to retrieve a string to use as a value for goBack() and other methods.

Next, we need to extend UrlManager , since all the request , response , Url:to() and goBack() methods and classes ultimately use UrlManager to complete the URL generation methods.

So create a new UrlManager. We will copy most of the code from the existing UrlManager by simply adding a little spice of our own. I saved mine in commands , but put it where you like, and change the namespace accordingly.

 <?php namespace app\commands; use Yii; use yii\web\UrlManager; class CustomUrlManager extends UrlManager { public function createUrl($params){ $request = Yii::$app()->request; $caller = $request->get('caller'); if ($caller && !$params['caller']){ $params['caller'] = $caller; } return parent::createUrl($params); } } 

So, iframe generates a caller parameter, and each link inside the iframe will also have a caller added as a parameter, since for a long time you used either Url::to() (or variants of this method) or Yii::$app->UrlManager for create links.

Now all we need to do is configure your controller's goBack () method to send any goBack () requests to the original source iframe.

 public function goBack($defaultUrl = null) { $caller = Yii::$app->request->get('caller'); if ($caller){ return Yii::$app->getResponse()->redirect($caller); } return Yii::$app->getResponse()->redirect(Yii::$app->getUser()->getReturnUrl($defaultUrl)); } 

Finally, you need to configure Yii to use your new UrlManager in your configuration file;

 'components' => [ 'urlManager' => [ 'class' => 'app/commands/CustomUrlManager' ] ] 

I would like to know if this works, it was an interesting task!

+1
source

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


All Articles