Zend Framework 2设置自定义布局和setTerminate问题 [英] Zend Framework 2 set custom layout and setTerminate issue

查看:129
本文介绍了Zend Framework 2设置自定义布局和setTerminate问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否是ZF2的错误,或者我只是不太了解它,但是我非常兴奋为什么会这样.

I dont know if this is a bug of ZF2 or I just dont understand it well, but Im very excited why is this happening.

我正在使用一种解决方案,通过附加Dispatch事件来全局更改每个模块的布局. (例如,来自 http ://framework.zend.com/manual/2.1/en/modules/zend.view.quick-start.html#dealing-with-layouts ,最后一个示例)

I'm using a solution to change layout of each module globaly by attaching a Dispatch event. (for example from http://framework.zend.com/manual/2.1/en/modules/zend.view.quick-start.html#dealing-with-layouts, last example)

它运作良好,但是问题是,当我要执行某些操作时 setTerminate(true); (用于Ajax调用)时,它不会仅显示 controller/action的内容模板,但只有布局模板没有内容!这就是我所期望的.

It's working well, but problem is, when in some action I want to setTerminate(true); (for Ajax call) it will not display only content of controller/action template, but only layout template without content! And that is what I'm not expecting.

这是模拟的方法,在分派函数中设置布局(而不是附加事件,以使其更加整洁),然后在控制器的操作中设置setTerminate.

This is how to simulate this, set layout in dispatch function (instead of attaching event, to make it cleaner) and then setTerminate in controller's action.

public function dispatch(Request $request, Response $response = null)
{
    parent::dispatch($request, $response);
    $this->layout('layout/new');
}   

public function indexAction()
{
    $model = new ViewModel();
    $model->setTerminal(true);
    return $model;
}

同样,我希望它仅显示 controller/index 模板的内容,而不是那,它仅显示 layout/new 的内容,而不包含内容

Again, I'm expecting that this will display only content of controler/index template, but instead of that, it display only content of layout/new without content.

我试图将布局设置为实际运行,并且按预期运行.

I tried to set layout in action, and it working how I'm expecting.

public function indexAction()
{
    $this->layout('layout/new');

    $model = new ViewModel();
    $model->setTerminal(true);

    return $model;
}

这是有效的,它仅显示 controller/index 模板的内容,而不显示布局.

This is working, it display only content of controller/index template and not layout.

因此,如果我要更改每个控制器的布局全局性(通过附加调度事件),它将一直起作用,直到我想将这些控制器之一用于Ajax调用并使用setTerminate.

So if I'm changing layout globaly (by attaching dispatch event) for each controller it working until I want to use one of these controllers for Ajax call and use setTerminate.

感谢您的帮助.

推荐答案

将视图模型标记为终端时,调度事件上的侦听器将布局视图模型替换为返回的视图模型.

When you mark your view model as terminal, listener on dispatch event replaces layout view model with view model you returned.

因此派遣后执行$this->layout('layout/new');为时已晚,您正在更改视图模型的模板.

So it is too late to do $this->layout('layout/new'); after dispatch, you are changing template of your view model.

您应该做的是附加监听器.例如,从控制器本身:

What you should do is attach listener. For example, from controller itself:

protected function attachDefaultListeners()
{
    //do not forget to call parent
    parent::attachDefaultListeners();

    $events = $this->getEventManager();
    //attach before action
    $events->attach(MvcEvent::EVENT_DISPATCH, array($this, 'changeLayout'), 10);
}

public function changeLayout(MvcEvent $event)
{
    $this->layout('layout/new');
}

这将为您的控制器设置布局,但您可以从操作中进行更改,并且setTerminal()将按预期工作

That will set layout for your controller, but you will be able to change it from action and setTerminal() will work as expected

这篇关于Zend Framework 2设置自定义布局和setTerminate问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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