How can i disable layout and view renderer in Zend Framework 2.x? I read documentation and can't get any answers looking in google i found answer to Zend 1.x and it's
$this->_helper->viewRenderer->setNoRender(true);
$this->_helper->layout->disableLayout();
But it's not working any more in Zend Framework 2.x. I need to disable both view renderer and layout for Ajax requests.
Any help would be great.
Just use
setTerminal(true)
in your controller to disable layout.This behaviour documented here: Zend View Quick Start :: Dealing With Layouts
Example:
If you want to send JSON response instead of rendering a .phtml file, try to use JsonRenderer:
Add this line to the top of the class:
and here an action example which returns JSON:
EDIT:
Don't forget to add
ViewJsonStrategy
to yourmodule.config.php
file to allow controllers to return JSON. Thanks @Remi!Slightly more info on the above answer... I use this often when outputting different types of files dynamically: json, xml, pdf, etc... This is the example of outputting an XML file.
The view is not rendered, and only the specified content and headers are sent.
You can add this to the end of your action: