2014-03-24 108 views
0

我傳遞函數_homepage索引,但它不傳遞$ data ['articles']。 任何人都可以幫我解決這個問題嗎? 此代碼更新CodeIgniter HMVC數據不從一個功能傳遞到另一個

 public function index(){ 
    $mydata = array(); 
    $this->mydata['menu'] = $this->Mdl_page->get_nested(); 
    $this->mydata['page'] = $this->Mdl_page->get_by(array('slug' => (string) $this->uri->segment(1)), TRUE); 
    count($this->mydata['page']) || show_404(current_url()); 

    //fetch page data 
    $method = '_' . $this->mydata['page']->template; 
    if (method_exists($this, $method)) { 
     $this->$method(); 
    } 
    else { 
     log_message('error', 'Could not load template ' . $method .' in file ' . __FILE__ . ' at line ' . __LINE__); 
     show_error('Could not load template ' . $method); 
    } 
    $this->mydata['subview'] = $data['page']->template; 
    //dump($data); 
    $this->load->view('_main_layout', $this->mydata); 

} 

private function _page(){ 
    dump('Welcome from the page template'); 
} 
private function _homepage(){ 
    $this->load->model('admin/Mdl_Articles'); 
    $this->db->limit(6); 
    $this->mydata['articles'] = $this->Mdl_Articles->get(); 
    //dump($data['articles']); 

} 
private function _news_archive(){ 
    dump('Welcome from the newspage template'); 
} 
+0

'$ data ['articles']'在方法'_homepage'的本地範圍內,除非返回'$ data'變量,否則''返回$ this-> Mdl_Articles-> get );'在你的索引方法中'$ data ['articles'] = $ this - > $ method();' ' –

+0

@Rahil Wazir它正在加載整個方法。所以它必須被檢索。反正我嘗試你的建議,但它不起作用 – user3456989

+0

Rahil我編輯這我不能這樣做你的方式,因爲我有其他方法給了我不同的結果。 – user3456989

回答

0

而是應該創建一個私有數組屬性

private mydata = array(); 

在你_homepage方法,內容應該是:

private function _homepage(){ 
    // ... 
    // target your class scope property 
    $this->mydata['articles'] = $this->Mdl_Articles->get(); 

,改變你的所有$data變量$this->mydata in index方法並將它傳遞給像這樣的視圖:

$this->load->view('_main_layout', $this->mydata); 
相關問題