我已经为我的CakePHP设置了REST,并且遇到了一些小问题。当调用一个GET方法时,比如我的控制器上的视图或索引,甚至是一个GET方法的自定义方法,我都没有问题得到答案。但是当我尝试像添加一样执行POST操作时,即使我可以看到它已正确到达并执行(保存到数据库),也不会从操作中获得输出。CakePHP - POST调用REST API返回null
我已经正确设置了JSON和XML输出的布局文件以及每种输出类型的路由和视图。
编辑:beforeFilter
相关的代码在AppController
:
if ($this->RequestHandler->isAjax()) {
$this->layout = 'ajax';
$this->autoRender = false;
} elseif ($this->RequestHandler->isXml()) {
$this->layout = 'default';
$this->RequestHandler->respondAs('xml');
$this->RequestHandler->renderAs($this, 'xml');
} elseif ($this->RequestHandler->ext == 'json') {
$this->layout = 'default';
$this->RequestHandler->respondAs('json');
$this->RequestHandler->renderAs($this, 'json');
} elseif ($this->RequestHandler->accepts('html')) {
$this->layout = 'frontend';
}
守则之路:我add
方法
Router::mapResources('fonykers');
Router::mapResources('voicenotes');
Router::parseExtensions();
相关的代码在FonykersController
:
$response = array('ok' => true, 'title' => 'Thank you for registering', 'msg' => 'A confirmation email has been sent to the provided email address, please click on the link in the email to complete the registration process');
if ($this->RequestHandler->isXml()) {
$this->set(compact('response'));
} else if ($this->RequestHandler->ext == 'json') {
pr('This prints');
$this->set(compact('response')); //View not being outputted
pr('This also prints');
} else {
echo json_encode($response);
}
我在JSON /views/fonykers/json
<?php echo json_encode(array('response' => $response)); ?>
我的布局文件视图:
<?php
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate");
header('Content-Type: application/json');
header("X-JSON: ".$content_for_layout);
echo $content_for_layout;
?>
没有看到相关的代码就无法说出任何内容。 – JJJ 2012-03-02 17:01:33
用相关代码更新了我的问题 – 8vius 2012-03-02 17:19:36