How To Display Controller Variable In View

Where is my mistake? I am getting empty window.

I want to display variable "theTime" from controller.php in the view.php:

protected\controllers\SomeController.php


class SomeController extends Controller

{

	public function actionHelloWorld()

	{

		$theTime = date("D M j G:i:s T Y");

		$this->render('helloWorld');

	}

... }

protected\views\some\helloWorld.php


...

<?php echo $this->actionHelloWorld()->theTime; ?>

...

Any controller property can be accessed by $this, so in Your case u need: $this->theTime;.

But U should set this var in controller and just populate it in your action:

class SomeController extends Controller

{

 &#036;theTime;


    public function actionHelloWorld()


    {


            &#036;this-&gt;theTime = date(&quot;D M j G:i:s T Y&quot;);


            &#036;this-&gt;render('helloWorld');


    }

… }

Then in veiew file U can do : $this->theTime;

or U can leave controller action as it is, just change this:

$this->render(‘helloWorld’, [‘theTime’=>$theTime]);

Than in view file U can do $theTime

Looks like a endless loop: actionHelloWorld -> view helloWorld.php -> actionHelloWorld -> view helloWorld.php …

IN controller ::

$this->render(‘helloWorld’,array(“theTime”=>$theTime));

In View

echo $theTime;

Thanks.

Replying to dragan.zivkovic

If i use in protected\controllers\SomeController.php


class SomeController extends Controller

{

	public function actionHelloWorld()

	{

		$this->theTime = date("D M j G:i:s T Y");

                $this->render('helloWorld');

        }

...

and in protected\views\some\helloWorld.php


<?php echo $this->theTime; ?>

i am getting the CException:


Property "MessageController.theTime" is not defined.


C:\xampp\htdocs\demo\protected\controllers\MessageController.php(7)


01 <?php

02 

03 class MessageController extends Controller

04 {

05     public function actionHelloWorld()

06     {

07         $this->theTime = date("D M j G:i:s T Y");

08 

09     //    $this->render('helloWorld',array('time'=>$theTime));

10         $this->render('helloWorld');

11     }

12 

13     public function actionIndex()

14     {

15         $this->render('index');

16     }

17     

18         public function actionGoodbye()

19     {


Stack Trace

#0 	

–

 C:\xampp\htdocs\demo\protected\controllers\MessageController.php(7): CComponent->__set("theTime", "Tue Jul 29 15:53:43 CEST 2014")


02 

03 class MessageController extends Controller

04 {

05     public function actionHelloWorld()

06     {

07         $this->theTime = date("D M j G:i:s T Y");

08 

09     //    $this->render('helloWorld',array('time'=>$theTime));

10         $this->render('helloWorld');

11     }

12 


#1 	

+

 C:\xampp\htdocs\yii\framework\web\actions\CInlineAction.php(49): MessageController->actionHelloWorld()

#2 	

+

 C:\xampp\htdocs\yii\framework\web\CController.php(308): CInlineAction->runWithParams(array("r" => "message/helloWorld"))

#3 	

+

 C:\xampp\htdocs\yii\framework\web\CController.php(286): CController->runAction(CInlineAction)

#4 	

+

 C:\xampp\htdocs\yii\framework\web\CController.php(265): CController->runActionWithFilters(CInlineAction, array())

#5 	

+

 C:\xampp\htdocs\yii\framework\web\CWebApplication.php(282): CController->run("helloWorld")

#6 	

+

 C:\xampp\htdocs\yii\framework\web\CWebApplication.php(141): CWebApplication->runController("message/helloWorld")

#7 	

+

 C:\xampp\htdocs\yii\framework\base\CApplication.php(180): CWebApplication->processRequest()

#8 	

–

 C:\xampp\htdocs\demo\index.php(13): CApplication->run()


08 defined('YII_DEBUG') or define('YII_DEBUG',true);

09 // specify how many levels of call stack should be shown in each log message

10 defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

11 

12 require_once($yii);

13 Yii::createWebApplication($config)->run();

you have to declare a public property in your controller


<?php


class MessageController extends Controller

{

     //  like so

     public $theTime;


     ... // rest of the code

}

Thanks for dragan.zivkovic and alirz23. Resuming the solution is:

protected\controllers\SomeController.php


class SomeController extends Controller

{

	public $theTime;


	public function actionHelloWorld()

	{ 

	$this->theTime= date("D M j G:i:s T Y");

	$this->render('helloWorld'); 

	}

....

protected\views\some\helloWorld.php


<?php echo $this->theTime; ?>