ZendFramework 2 中的控制器特定布局 [英] Controller specific layout in ZendFramework 2
问题描述
我的 zendframework 2 应用程序中有一个包含两个控制器的模块.我想为控制器的一项操作设置不同的布局.有没有办法在模块配置文件中设置它?
I have a module in my zendframework 2 application which contains two controllers. I want to set a different layout for one of the controller's actions. Is there a way to set it inside module config file?
P.s:我只是尝试使用以下命令将其设置在控制器的 __CONSTRUCT 方法中,但没有奏效!
P.s: I just tried to set it inside controller's __CONSTRUCT method using the following commands but it just didnt worked!
$event = $this->getEvent();
$event->getViewModel()->setTemplate('layout/MYLAYOUT');
但是如果我在控制器的每个动作中使用上述命令,它就可以正常工作.
But if i use the above commands inside each action of my controller it just works fine.
推荐答案
请参阅 akrabat 的示例,了解许多可以轻松调整布局、视图等的好方法.
See akrabat's examples for a number of nice ways that layouts, views, etcetera can be tweaked easily.
具体而言,您可以在 他的 github 在这里.
Specifically what you're looking for can be found on his github here.
这是设置/使用替代布局的控制器操作方法的剪切粘贴:
Here is a cut-paste of the controller's action method that sets/uses the alternate layout:
public function differentLayoutAction()
{
// Use a different layout
$this->layout('layout/different');
return new ViewModel();
}
<小时>
看起来 akrabat 有一个例子,上面写着 Change the layout for each action in a module
,这可能会提供在配置中设置布局的最佳指针;但我只是看了一下代码,这个例子目前还没有完成,它并没有改变布局.
It looks like akrabat has an example that says Change the layout for every action within a module
, which might give the best pointers for setting the layout in the config; but I just had a look at the code, and the example is currently unfinished, it's not changing the layout.
这篇关于ZendFramework 2 中的控制器特定布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!