Just use setTerminal(true)
in your controller to disable layout.
This behaviour documented here: Zend View Quick Start :: Dealing With Layouts
Example:
<?php
namespace YourAppController;
use ZendViewModelViewModel;
class FooController extends AbstractActionController
{
public function fooAction()
{
$viewModel = new ViewModel();
$viewModel->setVariables(array('key' => 'value'))
->setTerminal(true);
return $viewModel;
}
}
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:
use ZendViewModelJsonModel;
and here an action example which returns JSON:
public function jsonAction()
{
$data = ['Foo' => 'Bar', 'Baz' => 'Test'];
return new JsonModel($data);
}
EDIT:
Don't forget to add ViewJsonStrategy
to your module.config.php
file to allow controllers to return JSON. Thanks @Remi!
'view_manager' => [
'strategies' => [
'ViewJsonStrategy'
],
],
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…