如何在 Zend Framework 2.x 中禁用布局和查看渲染器?我阅读了文档,但在谷歌中找不到任何答案我找到了 Zend 1.x 的答案,它是
$this->_helper->viewRenderer->setNoRender(true);
$this->_helper->layout->disableLayout();
但它在 Zend Framework 2.x 中不再起作用。我需要为 Ajax 请求禁用 View 渲染器和布局。
任何帮助都会很棒。
最佳答案
只需使用 setTerminal(true)
在您的 Controller 中禁用布局。
此行为记录在此处:Zend View Quick Start :: Dealing With Layouts
例子:
<?php
namespace YourApp\Controller;
use Zend\View\Model\ViewModel;
class FooController extends AbstractActionController
{
public function fooAction()
{
$viewModel = new ViewModel();
$viewModel->setVariables(array('key' => 'value'))
->setTerminal(true);
return $viewModel;
}
}
如果您想发送 JSON 响应而不是呈现 .phtml 文件,请尝试使用 JsonRenderer:
将此行添加到类的顶部:
use Zend\View\Model\JsonModel;
这里是一个返回 JSON 的操作示例:
public function jsonAction()
{
$data = ['Foo' => 'Bar', 'Baz' => 'Test'];
return new JsonModel($data);
}
编辑:
不要忘记添加
ViewJsonStrategy
给您的 module.config.php
文件以允许 Controller 返回 JSON。谢谢@Remi!'view_manager' => [
'strategies' => [
'ViewJsonStrategy'
],
],
关于zend-framework - 如何在 ZF2 中禁用布局和 View 渲染器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18014885/