How to Render Zf2 View Within JSON Response

How to render ZF2 view within JSON response?

OK, i think i finally understood what you're doing. I've found a solution that i think matches your criteria. Though i am sure that there is room for improvement, as there's some nasty handwork to be done...

public function indexAction()
{
if (!$this->getRequest()->isXmlHttpRequest()) {
return array();
}

$htmlViewPart = new ViewModel();
$htmlViewPart->setTerminal(true)
->setTemplate('module/controller/action')
->setVariables(array(
'key' => 'value'
));

$htmlOutput = $this->getServiceLocator()
->get('viewrenderer')
->render($htmlViewPart);

$jsonModel = new JsonModel();
$jsonModel->setVariables(array(
'html' => $htmlOutput,
'jsonVar1' => 'jsonVal2',
'jsonArray' => array(1,2,3,4,5,6)
));

return $jsonModel;
}

As you can see, the templateMap i create is ... nasty ... it's annoying and i'm sure it can be improved by quite a bit. It's a working solution but just not a clean one. Maybe somehow one would be able to grab the, probably already instantiated, default PhpRenderer from the ServiceLocator with it's template- and path-mapping and then it should be cleaner.

Thanks to the comment ot @DrBeza the work needed to be done could be reduced by a fair amount. Now, as I'd initially wanted, we will grab the viewrenderer with all the template mapping intact and simply render the ViewModel directly. The only important factor is that you need to specify the fully qualified template to render (e.g.: "$module/$controller/$action")

I hope this will get you started though ;)

PS: Response looks like this:

Object:
html: "<h1>Hello World</h1>"
jsonArray: Array[6]
jsonVar1: "jsonVal2"

Correctly render ZF2 views inside JSON response?

public function fooAction()
{
$renderer = $this->getServiceLocator()->get('ViewRenderer');
$json = array();

foreach($list as $key => $item) {

$json[$key] = $item;

$content = new ViewModel(array('data' => $item));
$content->setTempalte('module/foo/bar');

$json[$key]['content'] = $renderer->render($content);
}

return new JsonModel($json);
}

zend framework 2, return the rendered contents of a view inside a JSON Model

you are returning the view model from an event I thinks this doesn't have any effect in current viewmanager view model, fetch the current viewmodel from viewmanager and call setTerminal(true). or replace the created jsonmodel using the viewmanager

How to change default rendering strategy to JSON in ZF2?

ZF2 raises an error by triggering specific events such as MvcEvent::EVENT_DISPATCH_ERROR or MvcEvent::EVENT_RENDER_ERROR when an exception is thrown.

The Zend\Mvc\View\Http\ExceptionStrategy class attaches a number of listens to these events so the HTML error page can be generated.

To return a JSON error message you can attach a your own custom exception strategy, with a higher priority, and check if the response should be JSON

ZF2: What's the easiest way to return a boolean json response?

Rob Allen has written an article about it:
Returning JSON from a ZF2 controller action

Also you can try this code to return every data without view rendering:

$response = $this->getResponse();
$response->setStatusCode(200);
$response->setContent('some data');
return $response;

How to render a different view in controller action of ZF2

can be done using

public function abcAction()
{
$view = new ViewModel(array('variable'=>$value));
$view->setTemplate('module/controler/action.phtml'); // path to phtml file under view folder
return $view;
}

Thanks to akrabat for covering almost every scenario.



Related Topics



Leave a reply



Submit