【问题标题】:disable layout for particular pages in ZF2禁用 ZF2 中特定页面的布局
【发布时间】:2014-06-12 19:04:26
【问题描述】:

如何禁用 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>

【问题讨论】:

    标签: layout zend-framework2


    【解决方案1】:

    对此有多种方法。此外 modules.zendframework 有很多模块 here 可以帮助你。

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

    <?php 
    //YourController.php
    public function someAction()
    {
       ...
       $this->layout()->footer = 'default';
       ...
    }
    
    //layout.phtml
    <?php if ($this->footer === 'default') : ?>
       //show the footer
    <?php endif; ?>
    

    虽然这样做是非常低效的。试想一下,您需要对所有控制器中的每个操作都执行此操作...我当然不想这样做。

    现在 zf2 有一个服务和事件层,可以在这里帮助我们很多。 This 是一本非常不错的读物和介绍。你只需编写一个服务并在你的控制器/路由/任何东西上触发一个事件。现在您可能还想配置显示的内容和隐藏的内容对吗?这也很容易。只需为自己编写一个配置文件并将其与 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;
    }
    

    来源:Where to put custom settings in Zend Framework 2?

    【讨论】:

    • 看起来 OP 只想隐藏 menu.phtml 而不是整个布局。
    • 只是要隐藏的 menu.phtml。
    • 哦。您是否在 layout.phtml 中使用了部分内容?
    【解决方案2】:

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

    $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; ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多