Redirect from one view to another view - Yii2

Choxx picture Choxx · Dec 28, 2015 · Viewed 16.6k times · Source

This is my form in the view page of my website.

<?= $form->field($model, 'taskID')->textInput(['readonly' => true, 'value' => Yii::$app->getRequest()->getQueryParam('id')])  ?>   

<?php 
$ifDistributor = User::find()->select('userType')->where(['username'=>Yii::$app->user->identity->username])->andWhere(['userType'=>'Distributer'])->exists();
$ifDistributorHasOnSiteSupport = Distributorinfo::find()->select('hasOnSiteSupport')->where(['UName'=>Yii::$app->user->identity->username])->andWhere(['hasOnSiteSupport'=>1])->exists();
if($ifDistributor)
 if($ifDistributorHasOnSiteSupport)
    echo $form->field($model, 'assignedToID')->dropDownList(
        ArrayHelper::map(dektrium\user\models\User::find()
            ->select('username')
            ->where(['userType'=>'CCE-Distributer'])
            ->andWhere(['distributerID'=>Yii::$app->user->getId()])
            ->all(),'username','username'),['prompt'=>'Select Person']
    );
else {  
                Yii::$app->session->setFlash('error', 
                    "Invalid Page");
                //I WANT TO REDIRECT TO index.php?r=tasks/index THIS URL                    

}                      
?>
<?= $form->field($model, 'remarks') ?>
<?= $form->field($model, 'scheduledTime')->widget(DateTimePicker::classname(), [
                'options' => ['placeholder' => 'Enter event time ...'],
                'pluginOptions' => [
                    'autoclose' => true
                ]
            ])  ?>
<div class="form-group">
    <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>

As shown above in the else part, I want to redirect to tasks/index this url. Help me how can I do that in this View only.

Answer

Insane Skull picture Insane Skull · Dec 28, 2015

Use Url::to() and don't forget to add yii\helpers\Url in Header.

For Example,

return Yii::$app->response->redirect(Url::to(['path', 'id' => id]));

Redirect()