In the controller, I want to change the layout from main to error. For this I write

$this->layout='error'; return $this->render('deny'); 

But still looking for the file deny.php in main. Why does not it switch to error?

    1 answer 1

    To change the layout, it is sufficient to define the property $layout in the controller.

    Example:

     class PostController extends Controller { public $layout = 'post'; // ... } 

    Source of

    Changing the layout does not mean that the directory containing the controller's view-elements will be changed. In order to change the directory in which the controller's view-elements will be contained, it is necessary to define the $viewPath property inside the controller (similar to the example above) or call the setViewPath() method in the body of one of the controller's actions.

    • For example, I have an error.php file in my frontend / views / error folder. when I write render ('error') in SiteController - it looks for the error.php file in the frontend / views / site folder. and I want him to look in the frontend / views / error folder. As you described above - will work? - Diefair
    • @Diefair, yes, in this case, you need to specify a new viewPath in the controller. - withoutname