Pass variables from controller for viewing in Yii

I can not use the variables specified in the controller in the corresponding view. Here is my code:

public function actionHelloWorld() { $this->render('helloWorld',array('var'=>'this is me')); } 

In helloWorld.php (view file):

 <h1>Hello, World!</h1> <h3><?php echo $var; ?></h3> 

It only displays β€œHello, World!”, It looks like this: $ var is not available in the view. Is anyone

+6
source share
2 answers

"var" is a reserved word in PHP, so you cannot use this name for your variable. See: http://www.php.net/manual/en/reserved.keywords.php

Try using a different variable name and it should work.

+5
source

which should work though with any variable name other than 'var'

note that 'this' in the view refers to its controller, so if you have a variable or public member method in the controller, you can access it from the view:

MyController.php:

 class MyController extends CController{ public $foo = 'bar'; public function actionIndex(){ $this->render('index'); } } 

index.php:

 <?php echo $this->foo; //result is bar ?> 
+4
source

Source: https://habr.com/ru/post/909911/


All Articles