Issues

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

Code tags added.

Added testing code

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

According to the comments this issue is fixed