2013-10-26 78 views
0

我使用CakePHP構建Web服務,並且我想爲所有控制器方法使用一個名爲output.ctp的視圖。到目前爲止,我發現我只能擁有一個與方法本身具有相同名稱的視圖。重用CakePHP控制器中所有方法的一個視圖

我這樣做是因爲我在輸出模板中有一個非常具體的代碼,需要在我發送的每個json文件中出現......任何人都可以提供幫助嗎? :)

回答

3

$this->render('output');在任何方法將強制該視圖呈現,而不論方法名稱。

$this->render('/OutputController/output');來自視圖控制器的外部。

雖然元素可能是一個更好的選擇,這取決於您嘗試實現的目標。

//output controller 
$this->render('output'); 

//posts controller 
$this->render('/Output/output'); 

編輯:陷入困境的類工作

<?php 

class AdminApiController extends AppController { 

    var $uses = array('Post', 'User', 'Application'); 

    public function posts() { 
     $this->layout = 'ajax'; 
     $this->set('data', $this->Post->find('all')); 
     $this->render('/Api/output'); 
    } 

    public function user() { 
     $this->layout = 'ajax'; 
     $id = $this->Auth->user('id'); 
     $this->User->id = $id; 
     $this->request->data = $this->User->read(null, $id); 
     unset($this->request->data['User']['password']); 
     unset($this->request->data['User']['password_token']); 
     $this->set('data', $this->request->data['User']); 
     $this->render('/Api/output'); 
    } 

    public function applications() { 
     $this->layout = 'ajax'; 
     $this->set('data', $this->Application->find('all')); 
     $this->render('/Api/output'); 
    } 
+0

非常好,謝謝你......會接受時,系統允許我這樣做(8分鐘)... :) – Ondrej

相關問題