ajax 如何在 Zend 框架 2 中禁用渲染视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12332843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable render view in zend framework 2?
提问by Tai T
I want to use some ajax, but I don't know how to use function as the same as setNoRender() in zend framework 2 to disable for render view.
我想使用一些 ajax,但我不知道如何使用与 zend 框架 2 中的 setNoRender() 相同的函数来禁用渲染视图。
How to disable rendering view in zend framework 2?
如何在 Zend 框架 2 中禁用渲染视图?
回答by Blanchon Vincent
To disable your view :
public function myactionAction() { // your code here ... return false; }
要禁用您的视图:
public function myactionAction() { // your code here ... return false; }
"return false" disables the view and not the layout! why? because the accepted types are:
“返回假”禁用视图而不是布局!为什么?因为接受的类型是:
- ViewModel
- array
- null
- 视图模型
- 大批
- 空值
so "false" disable the view.
所以“false”禁用视图。
To disable layout and view, return a response object:
public function myactionAction() { // your code here ... return $this->response; }To disable layout:
public function myactionAction() { // your code here ... $view = new ViewModel(); $view->setTerminal(true); return $view; }
要禁用布局和视图,请返回一个响应对象:
public function myactionAction() { // your code here ... return $this->response; }要禁用布局:
public function myactionAction() { // your code here ... $view = new ViewModel(); $view->setTerminal(true); return $view; }
回答by Rob Allen
If you're using JSON, then look at the view's JsonStrategyand return a JsonModelfrom you controller. See this article.
如果您使用的是 JSON,请查看视图JsonStrategy并JsonModel从控制器返回 a 。请参阅这篇文章。
Alternatively, you can return an Responsefrom your controller and the whole view layer is skipped:
或者,您可以Response从控制器返回 an并跳过整个视图层:
public function testAction()
{
$response = $this->getResponse();
$response->setStatusCode(200);
$response->setContent('foo');
return $response;
}
回答by Cmyker
Properand simple solution to do this
正确和简单的解决方案来做到这一点
public function testAction()
{
$data = array(
'result' => true,
'data' => array()
);
return $this->getResponse()->setContent(Json::encode($data));
}
Details: http://cmyker.blogspot.com/2012/11/zend-framework-2-ajax-return-json.html
详情:http: //cmyker.blogspot.com/2012/11/zend-framework-2-ajax-return-json.html
回答by Svyatoslav Tretyak
I found some answer.
我找到了一些答案。
Though $this->layout()->getLayout()returns the name/path of the newly selected layout... The layout does not change with any of the following commands...
虽然$this->layout()->getLayout()返回新选择的布局的名称/路径...布局不会随着以下任何命令而改变...
within a controller
在控制器内
$this->getLocator()->get('view')->layout()->setLayout('layouts/ajax.phtml');
$this->getLocator()->get('view')->layout()->setLayout('ajax');
$this->getLocator()->get('view')->layout()->disableLayout();
within a view PHTML file
在视图 PHTML 文件中
$this->layout()->setLayout('layouts/ajax.phtml');
$this->layout()->setLayout('ajax');
$this->layout()->disableLayout();
回答by Killan
...
use Zend\View\Model\JsonModel;
public function myAction() {
...
$view = new JsonModel($myArray);
$view->setTerminal(true);
return $view;
}
回答by Kdecom
$view = new ViewModel(); $view->setTerminate(true);
$view = new ViewModel(); $view->setTerminate(true);

