Zend 2 为控制器定义错误页面
Zend 2 define error page for controller
我有一个带有多个控制器的模块。我想对除一个控制器之外的所有控制器使用正常错误页面。
是否可以定义一个错误页面,仅在某个控制器抛出异常时使用?
这就是模块配置的样子:
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'layout/header' => __DIR__ . '/../view/layout/header.phtml',
'layout/footer' => __DIR__ . '/../view/layout/footer.phtml',
'layout/sidebar' => __DIR__ . '/../view/layout/sidebar.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'pagination/sliding' => __DIR__ . '/../view/pagination/sliding.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
我知道我可以通过将这个控制器放在一个额外的模块中并为此模块定义一个错误页面来实现类似的目的,但我不想将我的项目拆分成几个模块。
找到了解决我的问题的方法:
在template_map中定义错误模板:
'template_map' => array(
...
'error/controller' => __DIR__ . '/../view/error/controller/index.phtml',
),
在控制器构造方法中设置模板:
public function __construct()
{
$this->getServiceLocator()->get('ViewManager')->getExceptionStrategy()->setExceptionTemplate('error/controller');
}
我有一个带有多个控制器的模块。我想对除一个控制器之外的所有控制器使用正常错误页面。
是否可以定义一个错误页面,仅在某个控制器抛出异常时使用?
这就是模块配置的样子:
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'layout/header' => __DIR__ . '/../view/layout/header.phtml',
'layout/footer' => __DIR__ . '/../view/layout/footer.phtml',
'layout/sidebar' => __DIR__ . '/../view/layout/sidebar.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'pagination/sliding' => __DIR__ . '/../view/pagination/sliding.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
我知道我可以通过将这个控制器放在一个额外的模块中并为此模块定义一个错误页面来实现类似的目的,但我不想将我的项目拆分成几个模块。
找到了解决我的问题的方法:
在template_map中定义错误模板:
'template_map' => array(
...
'error/controller' => __DIR__ . '/../view/error/controller/index.phtml',
),
在控制器构造方法中设置模板:
public function __construct()
{
$this->getServiceLocator()->get('ViewManager')->getExceptionStrategy()->setExceptionTemplate('error/controller');
}