1

我正在使用 ZF3,我需要更改错误页面的布局。它们由应用程序modules.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.phtmlpages 设置特定的布局?

4

1 回答 1

3

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

$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) 并在不存在时设置特定布局的地方使用相同的逻辑。

于 2016-12-09T20:15:39.760 回答