render a view from another controller, yii
Asked Answered
S

2

29

The controller:

controllers
|-FooController.php
|-BarController.php

The views:

view
|-foo|
|    |-index.php
|    |-error.php
|
|-bar|
     |-index.php

How to render the error.php view with an action of the bar controller? I have tried:

$this->render('foo/error');

But it doesn't work.

Silicium answered 11/2, 2014 at 10:32 Comment(1)
try $this->render('/foo/error')Pellagra
M
65

try this

$this->render('//foo/error');
Myers answered 11/2, 2014 at 10:39 Comment(2)
How this is handled in Yii2 is documented here: yiiframework.com/doc-2.0/guide-structure-views.html#named-viewsLayne
I confirm this works in Yii2, in case you don't want to read the docSuperfuse
T
3

If you don't echo it, you will get a blank page. The correct way is

<?=
   $this->render('//foo/error');
?>

or

<?php
     echo $this->render('//foo/error');
?> 

This also works for Yii2

Threadgill answered 7/8, 2019 at 17:46 Comment(1)
In Yii2 it's also working with a single / at the beginning, like: <?= $this->render('/foo/error') ?>Flabby

© 2022 - 2024 — McMap. All rights reserved.