J'écris l'API REST et je souhaite retourner les réponses au format JSON. Donc, j'ai lu à propos de 'ContextSwitch'. Mais je ne peux pas le faire pour changer les en-têtes à 'application/json' et convertir les données en json.Zend Framework 1.12 et l'assistant 'ContextSwitch'
Voici le code de mon contrôleur:
public function preDispatch()
{
$this->getRequest()->setParam('format', 'json');
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
}
public function _init()
{
$contextSwitch = $this->_helper->getHelper('contextSwitch');
$contextSwitch ->addActionContext('post', 'json')
->initContext('json');
}
public function postAction()
{
echo 'test';
}
Quand je vérifie la réponse avec l'outil de ligne de commande Curl reçue:
< Content-Length: 4
< Content-Type: text/html
<
* Connection #0 to host localhost left intact
test* Closing connection #0
Pourquoi en-tête et les données ne sont pas modifiées? Comment puis-je le réparer?
Merci, le problème était en trait de soulignement. – Tamara