我正在使用CakePHP构建Web服务,我想为我所有的控制器方法使用一个名为output.ctp的视图。到目前为止,我发现我只能有一个视图必须与方法本身具有相同的名称。
我这样做是因为我在输出模板中有一个非常具体的代码,需要存在于我发送的每个 json 文件中......有人可以帮忙吗?:)
任何
方法中的$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');
}