如何在 zend-framework2 中设置自定义视图 [英] how to set a custom view in zend-framework2

查看:30
本文介绍了如何在 zend-framework2 中设置自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 zend framework2 中使用 ajax 返回自定义视图.此视图可能会根据 ajax 调用中的条件动态更改.. 那么我如何在 zf2 中通过 ajax 返回完整视图?

I am trying to return a custom view using ajax in zend framework2. This view may be changed dynamically according to the condition in ajax call.. So how i can return a full view through ajax in zf2 ?

推荐答案

您可以通过 ajax 返回完整视图,就像通过浏览器的位置栏返回完整视图一样.如果您只想返回 html,那么只需按照通常的方式返回一个视图模型.您可能想要禁用布局,这可以通过以下方式完成:

You can return a full view through ajax in the same way as you would return a full view through your browsers location bar. If you just want to return html, then simply return a view model in the same way you usually would. You may want to disable the layout, this can be done with:

$viewModel = new ViewModel();
$viewModel->setTerminal(true);
return $viewModel;

或者,如果你想返回 JSON,你可以使用 json 视图策略,它需要在你的 module.config.php 中启用:

Alternatively, if you want to return JSON, you can use the json view strategy, it needs to be enabled in your module.config.php:

'strategies' => array(
    'ViewJsonStrategy',
), 

然后,在您的操作中,返回一个新的 JsonModel 而不是 ViewModel.

then, in your action, return a new JsonModel instead of a ViewModel.

这篇关于如何在 zend-framework2 中设置自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆