【问题标题】:How to change layout for the error pages configured in modules.config?如何更改 modules.config 中配置的错误页面的布局?
【发布时间】:2017-04-20 08:10:06
【问题描述】:

我正在使用 ZF3,我需要更改错误页面的布局。它们在Applicationmodules.config文件中被如下配置调用:

   'view_manager' => [
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => [
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ],
        'template_path_stack' => [
            __DIR__ . '/../view',
        ],
    ],

如何为404.phtmlindex.phtml 页面设置特定布局?

【问题讨论】:

    标签: php layout zend-framework zend-framework3 zf3


    【解决方案1】:

    如果您想要特定操作的特定布局,您可以在控制器中使用:

    $this->layout()->setTemplate('layout/anOtherLayout');
    

    如果您希望控制器的所有操作具有相同的布局,您的控制器可以继承 AbstractActionController 并覆盖其 onDispatch() 方法:

    class IndexController extends AbstractActionController 
    {
        public function onDispatch(MvcEvent $e) 
        {
            // Call the base class onDispatch() first and grab the response
            $response = parent::onDispatch($e);        
    
            $this->layout()->setTemplate('layout/layout2');                
            // Return the response
            return $response;
        }
    }
    

    您的Module.php 可以使用相同的逻辑,您可以在其中检查路线是否存在 (404),如果不存在,则设置特定的布局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多