Pages

How To Redirect To Another Action In Yii2

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. Following example helps to implement redirection in Controller class.

namespace app\controllers

use Yii;

Class TestController extends Controller{
     public function actionTest(){
          $this->redirect(\Yii::$app->urlManager->createUrl("test/show"));
    }

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

In above example when we visit test/test then this action redirect to show method of the test class. We can redirect to any action of any controller by using this method. $this refers to the TestController and use redirect method of parent class and urlManager is ued to create valid url.

Unknown

Phasellus facilisis convallis metus, ut imperdiet augue auctor nec. Duis at velit id augue lobortis porta. Sed varius, enim accumsan aliquam tincidunt, tortor urna vulputate quam, eget finibus urna est in augue.

No comments:

Post a Comment