2014-02-17 41 views
0

PagesController.php顯示分類清單在主頁的蛋糕PHP

$this->loadModel('Category'); 
      $categories = $this->Category->find('all', 
       array(
       'limit' => 5, 
       'order' => 'Category.id ASC', 
       'fields' => array('Category.id','Category.category'), 
       'contain' => false, 
       'conditions' => array('Category.super_id' => 1), 
       'recursive' => 0 
       ) 
      ); 

     //pr($categories); Here showing all category listing 

     $this->set(compact('categories')); 
     //$this->set('categories',$categories); that code also used 

通過PagesController顯示分類清單在主頁的蛋糕PHP

但鑑於文件即時得到以下錯誤(鑑於/頁/ home.ctp)

<?php pr($categories); ?> 

Notice (8): Undefined variable: categories [APP/View/Pages/home.ctp, line 142] 

pagesController.php

public function display() { 
     $path = func_get_args(); 

     $count = count($path); 
     if (!$count) { 
      return $this->redirect('/'); 
     } 
     $page = $subpage = $title_for_layout = null; 

     if (!empty($path[0])) { 
      $page = $path[0]; 
     } 
     if (!empty($path[1])) { 
      $subpage = $path[1]; 
     } 
     if (!empty($path[$count - 1])) { 
      $title_for_layout = Inflector::humanize($path[$count - 1]); 
     } 
     $this->set(compact('page', 'subpage', 'title_for_layout')); 

     try { 
      $this->render(implode('/', $path)); 
     } catch (MissingViewException $e) { 
      if (Configure::read('debug')) { 
       throw $e; 
      } 
      throw new NotFoundException(); 
     } 

     $this->loadModel('Category'); 
     $categories = $this->Category->find('all', 
      array(
      'order' => 'Category.id ASC', 
      'fields' => array('Category.id','Category.category'), 
      'contain' => false, 
      'conditions' => array('Category.super_id' => 1), 
      'recursive' => 0 
      ) 
     ); 

     $this->set('categories',$categories); 
     //$this->set(compact('categories')); 
     //pr($categories); 



    } 

routes.php文件

Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); 
+0

@Wooble我沒有得到你的觀點,你能解釋一點點更多 –

+0

@Wooble使用CakePHP 2.3版本 –

+0

其中pagesController的行動是你嗎? – arilia

回答

1

編輯的迴應FULL PagesController:

我不認爲你的查找和一套正在執行(直到視圖渲染後),因爲它們是經過調用$ this-> render。嘗試移動他們是這樣的:

$this->loadModel('Category'); 
    $categories = $this->Category->find('all', 
     array(
     'order' => 'Category.id ASC', 
     'fields' => array('Category.id','Category.category'), 
     'contain' => false, 
     'conditions' => array('Category.super_id' => 1), 
     'recursive' => 0 
     ) 
    ); 

    $this->set('categories',$categories);   

    try { 
     $this->render(implode('/', $path)); 
    } catch (MissingViewException $e) { 
     if (Configure::read('debug')) { 
      throw $e; 
     } 
     throw new NotFoundException(); 
    } 

編輯的迴應-1: 請告訴我們你的整個PagesController.php,所以我們可以看到,如果有通過對home.ctp獲取數據的任何問題。您已經確定數據在您的控制器中的$ this-> set之前存在,但$ category實際上並未設置在視圖中('未定義變量'),因此這似乎是合乎邏輯的事情。

我知道這不是'答案',但我不允許'在原始帖子上評論',所以這是我可以傳達我的請求的唯一方法。

原始響應: 無法評論,因爲沒有足夠的聲譽,但你能告訴我們多一點你的頁面控制器,因爲我不知道該數據實際上沒有通過讓你的看法,因爲一些問題有..

+0

請參閱我的更新問題 –

+0

您是否嘗試過移動您的查找和設置語句以上try { $ this-> render(implode('/',$ path)); } – theotherdy

+0

請你能解釋更多,謝謝 –

1

因爲render呼叫您set語句之前

換句話說

你設置的類別中,頁面呈現

在行動開始移動你的代碼

+0

感謝您的好回答 –