Redirect from one view to another view - Yii2
Asked Answered
F

2

10

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.

Flummery answered 28/12, 2015 at 6:31 Comment(2)
return Yii::$app->response->redirect(Url::to(['path', 'id' => id])); Redirect()Trichotomy
@InsaneSkull That worked. But needs to use 'yii\helpers\Url;' Thanks for reply.. :)Flummery
T
12

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()

Trichotomy answered 28/12, 2015 at 7:4 Comment(1)
And adding id to the URL is optional.Flummery
C
7

Redirects are done in controllers or some related components, but not in views, because rendering doesn't make sense in this case.

In controller, you can use shorter form:

$this->redirect(['view', 'id' => $id]);

Note that you don't have to use Url::to() helper for building url because it's already applied internally.

You can also use:

Yii::$app->controller->redirect, if controller is unknown, this method calls Yii::$app->response->redirect as Insane Skull mentioned in his answer.

Take a look at the controller code generated by GII to see how redirect is used after saving / deletion.

Clothilde answered 28/12, 2015 at 9:23 Comment(3)
Thanks @arogachev. For more Info on this. +1 for that.Trichotomy
@Clothilde That's 100% correct that redirects are done in controllers to follow complete MVC architecture. But, In my case I needed to do that in view as per my requirement. I tried doing that in controller but there was some complexity issue. That's why I go up with view. I understood your scenario correctly and thanks for this nice answer and suggestion. +1 for that. :)Flummery
@choxx If you provide more details, maybe we can advice how to do it properly. There should be a way to do that, maybe through auxiliary component or event, but not in view. Glad to help anyway.Clothilde

© 2022 - 2024 — McMap. All rights reserved.