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');
}
... }
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();
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');
}
....