Returning JSON in a Zend Controller’s Action – Part 2

In a reply from my latest post after following the comments, there is a much elegant solution to this use case. Simply by changing the output with a JSON helper. This also changes the content-type of the returned response.

$data = array(...);
$this->_helper->json($data);

Related posts:

  1. Returning JSON in a Zend Controller’s Action
  2. JSON and Zend Framework? – Zend_Json
  3. Bind Zend Action with Non-Default View – Part 2
This entry was posted in micro tutorial, PHP, zend framework and tagged , , , , , , , , , , . Bookmark the permalink.

5 Responses to Returning JSON in a Zend Controller’s Action – Part 2

  1. Richard Tuin says:

    Nice! Now that makes things interesting, thank you for sharing.

  2. Thanks for the correction. If your action is going to return just JSON and always JSON, the Json Helper is the best option, in case your action can return different formats (Xml, Json, Yaml, etc.), ContextSwitch or AjaxContext are better.

    Best!

  3. Francisco says:

    My bad, i forgot to translate this.

    I’m looking for a long time, for a way to return the ID of an element selected from the autocomplete.

    If you have any feedback, please send me an email.

  4. j3r says:

    Sorry i don’t really get how u to it. Do u still need a view script for this second method u psoted?

    for example i have an action named
    public function getAllJsonAction()
    {
    $data = array();
    $this->_helper->json($data);
    }

    Do i still need a getAllJson.phtml???

    Cheers! Many thanks!

  5. Stoimen says:

    @j3r – No, you don’t need a view script!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">