Я настроил REST для моего CakePHP, и у меня небольшая проблема.При вызове метода GET, такого как view или index на моем контроллере, или даже пользовательского метода GET, у меня нет проблем с получением ответа.Но когда я пытаюсь выполнить действие POST, например, add, я не получаю вывод от операции, хотя вижу, что она была выполнена правильно и выполнена (сохранена в БД).
Я правильно настроил макетфайл для вывода 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';
}
Код в маршрутах:
Router::mapResources('fonykers');
Router::mapResources('voicenotes');
Router::parseExtensions();
Соответствующий код в моем add
методе в 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);
}
Мой взгляд в /views/fonykers/json
<?php echo json_encode(array('response' => $response)); ?>
Мой файл макета для json:
<?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;
?>