4
votes

I try to do something like a standard behaviour in Yii2 if the view file doesn't exist. For example if view 'xyz' not exist then redirect to another controller action or would it be easier if I just render then a standard view like a special 404 page?

1

1 Answers

6
votes

You may do in your controller as below:

<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\base\ViewNotFoundException;

class ExampleController extends Controller
{
    public function actionIndex()
    {
        // ...

        try
        {
            return $this->render('your-view', [
                // ...
            ]);
        }
        catch (ViewNotFoundException $e)
        {
            $this->redirect();
        }
    }

}

?>