Layout 禁用ZF2中特定页面的布局

Layout 禁用ZF2中特定页面的布局,layout,zend-framework2,Layout,Zend Framework2,如何在ZF2中为控制器中的特定页面禁用特定布局(例如:menus.phtml)??在下面的示例中,应禁用特定页面的menus.phtml。其余页面必须包含类似menus.phtml的页眉和页脚 <div> header.phtml </div> <div> menus.phtml </div> <div> <?php echo $this->content; ?> </div> &l

如何在ZF2中为控制器中的特定页面禁用特定布局(例如:menus.phtml)??在下面的示例中,应禁用特定页面的menus.phtml。其余页面必须包含类似menus.phtml的页眉和页脚

<div>
    header.phtml
</div>
<div>
    menus.phtml
</div>
<div>
    <?php echo $this->content; ?>
</div>
<div>
    footer.phtml
</div>

header.phtml
menus.phtml
footer.phtml

首先,获取控制器或操作名称:

$controllerName =$this->params('controller');
$actionName = $this->params('action');
然后在布局/视图脚本中添加一个简单的逻辑

<?php if ($actionName != 'action that you want to disable the layout/menu'): ?>
    echo $this->render('menus.phtml');
<?php endif; ?>

echo$this->render('menus.phtml');

对此有多种方法。此外,modules.zendframework有许多模块可以帮助您解决问题

如果您仍然热衷于自己编写,您可以在控制器内的布局中添加变量,如下所示:

<?php 
//YourController.php
public function someAction()
{
   ...
   $this->layout()->footer = 'default';
   ...
}

//layout.phtml
<?php if ($this->footer === 'default') : ?>
   //show the footer
<?php endif; ?>

//显示页脚
但这样做效率很低。想象一下,你需要对所有控制器中的每个动作都这样做。。。我当然不想那样做

现在,zf2有一个服务和事件层,可以在这里帮助我们解决很多问题是一本非常好的读物,并对其进行了介绍。您只需编写一个服务并在控制器/路由/任何东西上触发一个事件。现在您可能还想配置显示的内容和隐藏的内容,对吗?这也很容易。只需编写一个配置文件并将其与global.config合并,如下所示:

<?php
//CustomModule/module.php
public function getConfig() {
   $config = array();
   $configFiles = array(
      include __DIR__ . '/config/module.config.php',
      include __DIR__ . '/config/module.customconfig.php',
   );
   foreach ($configFiles as $file) {
      $config = \Zend\Stdlib\ArrayUtils::merge($config, $file);
   }
   return $config;
}

看起来好像OP只想隐藏
菜单.phtml
而不是整个布局。只想隐藏menu.phtml.Ohhh。您是否在layout.phtml中使用partials?