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.

Choxx
  • 945
  • 1
  • 24
  • 46
  • 1
    `return Yii::$app->response->redirect(Url::to(['path', 'id' => id]));` [Redirect()](http://www.yiiframework.com/doc-2.0/yii-web-response.html#redirect%28%29-detail) – Insane Skull Dec 28 '15 at 06:38
  • @InsaneSkull That worked. But needs to use 'yii\helpers\Url;' Thanks for reply.. :) – Choxx Dec 28 '15 at 06:45

2 Answers2

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

Insane Skull
  • 9,220
  • 9
  • 44
  • 63
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.

arogachev
  • 33,150
  • 7
  • 114
  • 117
  • 1
    Thanks @arogachev. For more Info on this. +1 for that. – Insane Skull Dec 28 '15 at 09:29
  • @arogachev 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. :) – Choxx Dec 29 '15 at 05:01
  • 1
    @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. – arogachev Dec 29 '15 at 05:36