2017-03-02 109 views
-2

我正在學習PHP:分離關注點,MVC,函數,數組,作品。誠然,我現在迷失了。PHP MVC - 未定義變量

我使用的是現有的認證框架:

https://github.com/panique/huge/ 
// app/core/View.php 
/** 
* Class View 
* The part that handles all the output 
*/ 
class View 
{ 
    /** 
    * simply includes (=shows) the view. this is done from the controller. In the controller, you usually say 
    * $this->view->render('help/index'); to show (in this example) the view index.php in the folder help. 
    * Usually the Class and the method are the same like the view, but sometimes you need to show different views. 
    * @param string $filename Path of the to-be-rendered view, usually folder/file(.php) 
    * @param array $data Data to be used in the view 
    */ 
    public function render($filename, $data = null) 
    { 
     if ($data) { 
      foreach ($data as $key => $value) { 
       $this->{$key} = $value; 
      } 
     } 

     require Config::get('PATH_VIEW') . '_templates/header.php'; 
     require Config::get('PATH_VIEW') . $filename . '.php'; 
     require Config::get('PATH_VIEW') . '_templates/footer.php'; 
    } 

上面是一張紙條告知您的視圖類。

在我IndexCotroller:

public function index() 
    { 
     $this->View->render('index/index', array(
      'images' => ImagesModel::getImages()) 
     ); 
    } 

在我ImageModel:

class ImageModel 
{ 
    /** 
    * 
    * 
    */ 
    public static function getImages($xxx) 
    { 
     $xxx = "test test test"; 
    } 
} 

在索引/索引視圖我有:

<?php $this->images; ?> 

我得到一個錯誤,如:

Warning: Missing argument 1 for ImageModel::getImages(), called in IndexController.php on line 21 and defined in ImageModel.php on line 13 

我只想回顯一個變量來測試我可以從我的模型中獲取數據到我的索引視圖。我對PHP非常陌生,當然比我能咀嚼的東西要嚼得多,但是這個挑戰讓我不得不知道。

我的函數和數組是否古怪?火焰我,但也提供知識?!

+1

' images; ?>應該是回聲嗎?因爲它看起來什麼都不做 – RiggsFolly

+1

'ImagesModel :: getImages()'你用參數定義了方法!但是你沒有通過這個電話。 _Should該方法'返回'的東西,而不是設置'$ xxx = somthing_ – RiggsFolly

+1

_I只是想回顯一個變量,以測試我可以從我的模型中獲取數據到我的索引視圖_替換'$ xxx =「test test test; '用'返回'測試測試測試';'至少現在,在該方法上鬆開參數 – RiggsFolly

回答

0

那麼,首先,你不會從ImagesModel::getImages()返回一些東西,這樣值就不會被設置。其次,您未通過getImages()中的參數,但您在ImageModel中有此參數。所以你應該從getImage()返回一些圖像,並刪除該參數(或傳遞參數值,保持參數)使其工作。