ZF2查看策略 [英] ZF2 View strategy

查看:164
本文介绍了ZF2查看策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下操作:



简单的控制器和操作。行动应根据请求返回2种类型的响应:

 在普通请求(text\html)的情况下,HTML b $ b在ajax请求的情况下的JSON(application \json)

我已经设法做这通过一个控制器的插件,但这需要写入

  return $ this-> myCallBackFunction($ data)$ b $在每个动作中,b  

如果我不会对整个控制人这样做呢?试图找出如何通过事件侦听器来实现它,但是无法成功。



任何提示或链接到某些文章将不胜感激!

$ b

.html#zend-mvc-controller-plugins-acceptableviewmodelselectorrel =noreferrer>可接受的视图模型选择器控制器插件专门用于此目的。它将根据您通过查看由客户端发送的Accepts标头定义的映射来选择一个适当的ViewModel。



对于您的示例,您首先需要启用JSON视图通过将其添加到您的视图管理器配置(通常在 module.config.php )中的策略:

 'view_manager'=>数组(
'strategies'=>数组(
'ViewJsonStrategy'

),

(可能您已经有一个view_manager键,在这种情况下,将策略部分添加到您当前的配置中。)



然后在控制器中调用控制器插件,使用映射作为参数:

  class IndexController extends AbstractActionController 
{
protected $ acceptMapping = array(
'Zend\View\Model\ViewModel'=>数组(
'text / html'
) ,
'Zend\View\Model\JsonModel'=> array(
'application / json'

);

public function indexAction()
{
$ viewModel = $ this-> acceptableViewModelSelector($ this-> acceptMapping);

return $ viewModel;
}
}

这将为标准请求返回一个普通的ViewModel,一个用于接受JSON响应(即AJAX请求)的请求的JsonModel。



您分配给JsonModel的任何变量将显示在JSON输出中。


I'm trying to implement the following:

Simple controller and action. Action should return response of 2 types depending on the request:

HTML in case of ordinary request (text\html),
JSON in case of ajax request (application\json)

I've managed to do this via a plugin for controller, but this requres to write

return $this->myCallBackFunction($data)

in each action. And what if I wan't to do this to whole controller? Was trying to figure out how to implement it via event listener, but could not succed.

Any tips or link to some article would be appreciated!

解决方案

ZF2 has the acceptable view model selector controller plugin specifically for this purpose. It will select an appropriate ViewModel based on a mapping you define by looking at the Accepts header sent by the client.

For your example, you first need to enable the JSON view strategy by adding it to your view manager config (typically in module.config.php):

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

(It's likely you'll already have a view_manager key in there, in which case add the 'strategies' part to your current configuration.)

Then in your controller you call the controller plugin, using your mapping as the parameter:

class IndexController extends AbstractActionController
{
    protected $acceptMapping = array(
        'Zend\View\Model\ViewModel' => array(
            'text/html'
        ),
        'Zend\View\Model\JsonModel' => array(
            'application/json'
        )
    );

    public function indexAction()
    {
        $viewModel = $this->acceptableViewModelSelector($this->acceptMapping);

        return $viewModel;
    }
}

This will return a normal ViewModel for standard requests, and a JsonModel for requests that accept a JSON response (i.e. AJAX requests).

Any variables you assign to the JsonModel will be shown in the JSON output.

这篇关于ZF2查看策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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