2011-05-27 143 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> 

它適用於我,並且我明白代碼中發生了什麼。作爲一個新手,這很重要:要知道你在做什麼,並實施它。