View Source

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFDEV:Zend Proposal Zone Template}

{zone-data:component-name}
Zend_View_Helper_Json
{zone-data}

{zone-data:proposer-list}
[~matthew]
{zone-data}

{zone-data:revision}
0.9.0 - 03 January 2008: Initial proposal release
0.9.1 - 04 January 2008: Initial code released on laboratory
0.9.2 - 08 January 2008: Updated with feedback from Zend team; no longer exits; adds action helper version; disables layouts
{zone-data}

{zone-data:overview}
Zend_View_Helper_Json is a view helper for serializing data to JSON and sending an immediate JSON response to the client.
{zone-data}

{zone-data:references}
* Zend_Json
* [Zend_View_Helper_Json laboratory code|http://framework.zend.com/svn/laboratory/Zend_Form/library/Zend/View/Helper/Json.php]
* [Zend_View_Helper_Json laboratory tests|http://framework.zend.com/svn/laboratory/Zend_Form/tests/Zend/View/Helper/JsonTest.php]
{zone-data}

{zone-data:requirements}
* This component *will* serialize received data to JSON.
* This component *will* set appropriate HTTP response headers for returning JSON data.
* This component *will* provide both an action helper and a view helper, preferably using common backend functionality.
* This component *will* disable layouts, if enabled.
* This component *will not* exit immediately; that will be left to the developer to implement.
{zone-data}

{zone-data:dependencies}
* Zend_Json
* Zend_Controller_Response_Abstract (via Zend_Controller_Front)
* Zend_Controller_Action_Helper_Abstract
{zone-data}

{zone-data:operation}
The helper is called as a normal view helper, serializes the data passed to it, sets the appropriate response header, disables layouts, and returns the serialized JSON.

Called as an action helper, it does all of the above, as well as sets the reponse body to the serialized JSON data.
{zone-data}

{zone-data:milestones}
* Milestone 1: \[ZFPROP:DONE\] Create proposal for review
* Milestone 2: \[ZFPROP:DONE\] Create working tests and code in laboratory
* Milestone 3: Move working tests and code to incubator
* Milestone 4: Create documentation in incubator
* Milestone 5: Publish to core

{zone-data}

{zone-data:class-list}
* Zend_View_Helper_Json
* Zend_Controller_Action_Helper_Json
{zone-data}

{zone-data:use-cases}
||UC-01||
In a view script:
{code:php}
<?= $this->json($this->data) ?>
{code}

||UC-02||
In an action controller:
{code:php}
class FooController extends Zend_Controller_Action
{
public function indexAction()
{
// ...
if ('json' == $this->_helper->ajaxContext->getCurrentContext()) {
return $this->_helper->json($data);
}
// ...
}
}
{code}
{zone-data}

{zone-data:skeletons}
h2. Zend_View_Helper_Json
{code:php}
class Zend_View_Helper_Json
{
public function json($data);
}
{code}

h2. Zend_Controller_Action_Helper_Json
{code:php}
class Zend_Controller_Action_Helper_Json extends Zend_Controller_Action_Helper_Abstract
{
public function encode($data);
public function direct($data); // proxies to encode()
}
{code}
{zone-data}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>