ZF2-213: JsonModel cannot be used as layout during rendering

Issue Type: Bug Created: 2012-03-11T16:17:11.000+0000 Last Updated: 2012-10-08T20:15:24.000+0000 Status: Closed Fix version(s): Reporter: Yonni Mendes (yonman) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend\View

  • JsonRenderer
  • ViewModel

Related issues: Attachments:


My use case required I create a compound Json response, part of which is a standard json wrapper to variable json content.

During an application event I called the overrode the default view model:

<pre class="highlight">
$layout = new \Zend\View\Model\JsonModel(array(... variables... and a captureTo placeholder));

And added the JsonStrategy

<pre class="highlight">
$view->events()->attach($locator->get ( 'Zend\View\Strategy\JsonStrategy' ), 100);

In my controller I returned a different JsonModel with its own parameters, fully expecting to see the array I introduced in the controller output in the captureTo placeholder ... but this doesn't work.

A quick look in JsonRenderer:115-124 shows why:

<pre class="highlight">
        if ($nameOrModel instanceof Model) {
            if ($nameOrModel instanceof Model\JsonModel) {
                $values = $nameOrModel->serialize();
            } else {
                $values = $this->recurseModel($nameOrModel);
                $values = Json::encode($values);

            return $values;

If a JsonModel is sent to the renderer directly, it is serialized. This serialization completely ignores its children (it does, however, parse internal variables). Seems to me this kind of shoots my use case in the foot and requires a phtml view script always wrap the json output ... which is weird.

recurseModel works as expected, though I seemed to have a hard time having the children rendered anywhere but a variable named "content".


Posted by Ralph Schindler (ralph) on 2012-10-08T20:15:24.000+0000

This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit:

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.