php Zend Framework 2.0 控制器中的更改布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9112766/
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
Change layout in the controller of Zend Framework 2.0
提问by Alex Pliutau
I am learning ZF2.0 beta. In ZF1 we can change layout on the fly in controller:
我正在学习ZF2.0 测试版。在 ZF1 中,我们可以在控制器中动态更改布局:
Zend_Layout::getMvcInstance()->setLayout('layoutname');
But in ZF2.0Zend\Layout class doesn't has method getMvcInstance(). I think that it can be made using dependency injections but not sure.
但是在ZF2.0Zend\Layout 类中没有 getMvcInstance() 方法。我认为它可以使用依赖注入来实现,但不确定。
回答by Jurian Sluiman
The ZF2 is heavily under development and no guarantee can be made the way it works now, will be the way it works when ZF2 reaches a stable state.
ZF2 正在大量开发中,不能保证它现在的工作方式,当 ZF2 达到稳定状态时会是它的工作方式。
However, the new view layer from Zend\Mvc is recently merged so you should be able to do this now (with current master):
但是,来自 Zend\Mvc 的新视图层最近已合并,因此您现在应该能够执行此操作(使用当前主控):
public function somethingAction ()
{
// Do some intelligent work
$this->layout('layout/different');
}
回答by TALLBOY
The best way I've found to set templates in actions is like this
我发现在动作中设置模板的最好方法是这样的
public function someAction() {
$viewModel = new ViewModel();
$viewModel->setTemplate('layout/custom');
return $viewModel;
}
In your module.config.php
make sure you've set your appropriate template_map
path.
在你module.config.php
确保你已经设置了合适的template_map
路径。
'view_manager' => array(
'template_map' => array(
'layout/custom' => __DIR__ . '/../view/layout/custom.phtml'
),
),
回答by Aurel Avramescu
public function someAction() {
$layout = $this->layout();
$layout->setTemplate('layout/custom');
$viewModel = new ViewModel();
return $viewModel;
}
回答by 2plus
I have tried the above tips.
我已经尝试了上述提示。
public function somethingAction ()
{
// Do some intelligent work
$this->layout('layout/different');
}
I got the correct result with this snippet.
我用这个片段得到了正确的结果。
public function someAction() {
$viewModel = new ViewModel();
$viewModel->setTemplate('layout/custom');
return $viewModel;
}
It fetched both layouts(default & current module).
它获取了两种布局(默认和当前模块)。
回答by RedPhoenix
You will also have to set the layout either in the bootstrap or when using di. Di example:
您还必须在引导程序中或使用 di 时设置布局。示例:
'Zend\View\Resolver\TemplateMapResolver' => array(
'parameters' => array(
'map' => array(
'layout/different' => __DIR__ . '/../view/layout/different.phtml',
),
),
),