2011-05-27 187 views
0

在控制器(它是一种称为MessageController类),有该代码,这使得一个“视图”的文件叫做helloWorld并且还设置其中变量$ theTime连接到所述阵列关键'时间'。设置公共类属性

$theTime = date("D M j G:i:s T Y"); 
$this->render('helloWorld',array('time'=>$theTime)); 

在视图helloWorld的文件,显示在这里通过变量$时间

<h3><?php echo $time; ?></h3> 

这工作完全来自控制器的关键“时间”。但是,这本书也建议尝试另一种方式。它说

Alter the previous example by defining a public class property on MessageController, rather than a locally scoped variable, whose value is the current date and time. Then display the time in the view file by accessing this class property through $this.

我一直无法弄清楚如何做到这一点。任何人都知道如何

回答

3
class MessageController { 
    public $time; 

    public function beforeAction($action) { 
    $this->time = date("D M j G:i:s T Y"); 
    return true; 
    } 

    public function someAction() { 
    $this->render('helloWorld'); 
视图

echo $this->time; 
+0

我不认为你可以有一个表达式评估这样的变量,但我尝试了你的建议,并且我得到了这个错误 解析错误:语法错误,意外的'(',期待','或'; “在/Applications/MAMP/htdocs/demo/protected/controllers/MessageController.php第6行 – Leahcim 2011-05-27 09:42:14

+0

我搞砸与代码,而现在编辑。没有在'Yii'代码为过长...的时间,我想你最好把'$这 - >时间=日期( “DMĴG:我:■TY”);''中beforeAction($动作)'方法 – Nemoden 2011-05-27 09:44:30

+1

@Nemoden此(你张贴第一方式)不是Yii的特异性。你不能默认值分配给属性,如果它不是一个标量。[看更多的特性在PHP 5](http://php.net/manual/en/language.oop5.properties.php)。但现在它是确定,如果'beforeAction()被执行' – Tadeck 2011-05-27 09:59:05

0
// I defined $MyClassTime as a public class variable in "MessageController.php" 
    //as follows: 

    class MessageController extends Controller 
    { 
     public $MyClassTime; 

     public function actionHelloWorld() 
     { 
      $this->MyClassTime = "From Public Class Property: " . date("D M j G:i:s T Y");  

      $this->render('helloWorld'); 

     } 

     public function actionIndex() 
     { 
      $this->render('index'); 
     } 

    // And then did this in "helloWorld.com": 

     <?php 
     $this->breadcrumbs=array(
      'Message'=>array('message/index'), 
      'HelloWorld', 
     );?> 
     <h1>Hello, World!!</h1> 
     <h3><?php echo $this->MyClassTime; ?></h3> 
0

在控制器/ MessageController.php文件

class MessageController extends Controller 
    { 
     public $theTime; 

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

     public function actionHelloWorld() 
     { 
      $this->render('helloWorld',array('time'=>$this->theTime)); 
     } 
    } 

在视图/消息/ helloWorld.php

<h3><?php echo $time; ?></h3><hr/> 
0

好吧,书中的指示明确写着:“由MessageController定义一个公共类特性改变前面的例子......然后通过$此访问该类物业在视图中显示文件所需的时间。

话虽这么说,这是我想出了:

在MessageController.php:

  class MessageController extends Controller 
      { 
       public $defaultAction = 'hello'; 
       public $theTime; // as per book's instructions 

       public function actionHello() 
       { 
        $this->theTime = date("D M j G:i:s T Y"); 
        $this->render('hello'); 
       } 

在保护/视图/消息/ hello.php:

  <h1>Hello, World!</h1> 
      <h3> 
      <?php echo $this->theTime; ?> 
      </h3> 

它适用于我,并且我明白代码中发生了什么。作为一个新手,这很重要:要知道你在做什么,并实施它。