Menus

Thursday 8 September 2016

Redirect to another action in YII2


REDIRECT TO ANOTHER ACTION IN YII2


yii2 redirect in controller action does not work?



$this->redirect()


While developing web application we need to redirect to different url in our application.

Redirect helps us to load different url from different location.

In Yii2 framework we can do it through redirect method of Controller class.


Redirect to action to same or different action in controller

$this->redirect(\Yii::$app->urlManager->createUrl("controllerName/actionName"));

$this->redirect(\Yii::$app->urlManager->createUrl("moduleName/controllerName/actionName"));

Send extra parameter then use redirect.


$this->redirect(\Yii::$app->urlManager->createUrl(array("master/studentsstudymaterials/despatch", 'id'=>10)));



public function actionName1($param1,$param2) {

    //your code...

}

public function actionName2() {

    $this->redirect(array('name1','param1'=>'param1','param2'=>'param2'));

}




Following example helps to implement redirection in Controller class.


namespace app\controllers

use Yii;

Class TestController extends Controller{


     public function actionIndex(){
          $this->redirect(\Yii::$app->urlManager->createUrl("test/show"));
    }

    public function actionShow(){
       //your action here
   }


}

In above example when we visit test/index then this action redirect to show method of the test class. 


More Details

No comments:

Post a Comment