ZF2-183: Zend\View\Renderer\JsonRenderer -> recurseModel() returns Zend\View\Variables instead of an array
Description
/**
* Retrieve values from a model and recurse its children to build a data structure
*
* @param Model $model
* @return array
*/
The recurseModel(Model $model) function in Zend\View\Renderer\JsonRenderer does not return an array. instead it returns:
Zend\View\Variables Object
(
[strictVars:protected] =>
[storage:ArrayObject:private] => Array
(
[content] => Array
(
[test] => json
)
)
)
class Module implements AutoloaderProvider {
public function init(Manager $moduleManager) {
$events = StaticEventManager::getInstance();
$events->attach('bootstrap', 'bootstrap', array($this, 'initializeView'), 100);
$events->attach('bootstrap', 'bootstrap', array($this, 'initializeJsonView'));
}
public function initializeJsonView($e) {
$app = $e->getParam('application');
$locator = $app->getLocator();
$view = $locator->get('Zend\View\View');
$jsonStrategy = $locator->get('Zend\View\Strategy\JsonStrategy');
$view->events()->attachAggregate($jsonStrategy,10);
}
public function initializeView($e) {
$app = $e->getParam('application');
$basePath = $app->getRequest()->getBasePath();
$locator = $app->getLocator();
$renderer = $locator->get('Zend\View\Renderer\PhpRenderer');
$renderer->plugin('url')->setRouter($app->getRouter());
$renderer->doctype()->setDoctype('HTML5');
$renderer->plugin('basePath')->setBasePath($basePath);
}
}
class UserController extends ActionController {
public function indexAction() {
return array('json' => 'test');
}
}
Comments
Posted by Frank Brückner (frosch) on 2012-03-01T11:26:11.000+0000
Code tags added.
Posted by Roman Vidyayev (cmple) on 2012-03-01T17:34:48.000+0000
Added testing code
Posted by Matt Kynx (kynx) on 2012-04-22T16:21:38.000+0000
It looks like this has been fixed in recent git versions, though it still outputs the view's 'captureTo' as a key if the controller returns a normal ViewModel (ie something like {'content':{'json':'test'}}).
Returning a JsonModel works as expected (it's got captureTo set to null), but that kind of defeats the purpose of overriding the renderer in the first place. See: http://framework.zend.com/issues/browse/ZF2-213
Posted by Maks 3w (maks3w) on 2012-06-14T21:10:35.000+0000
According to the comments this issue is fixed