Zend Framework 2 中,定制error 的layout

来源:互联网 发布:北京java培训班学费 编辑:程序博客网 时间:2024/06/06 05:36

http://my.oschina.net/phpyer/blog/133347

404页面在我们平常浏览网页的时候并不会经常碰到,也因此很容易被忽略,在Zend Framework 2中,项目的结构已经默认提供了404 页面,包括样式等等。但是有时,我们需要定制符合自己网站的404 页面样式时,该怎么办呢?

其实非常简单,编辑错误页面的layout ,例如errorlayout

module.config.php

view source
print?
01'view_manager' => array (
02        'display_not_found_reason' => true,
03        'display_exceptions' => true,
04        'doctype' => 'HTML5',
05        'not_found_template' => 'error/404',
06        'exception_template' => 'error/index',
07        'template_path_stack' => array (
08            __DIR__ . '/../view'
09        ),
10        'template_map' => array (
11            'error/404' => __DIR__ . '/../view/error/404.phtml',
12            'error/index' => __DIR__ . '/../view/error/index.phtml',
13            'error/layout'=> __DIR__ . '/../view/layout/error_layout.phtml'
14        )
15    ),
module.php
01class Module
02{
03    public function onBootstrap(MvcEvent $e)
04    {
05        $e->getApplication()->getServiceManager()->get('translator');
06        $eventManager        $e->getApplication()->getEventManager();
07        $moduleRouteListener new ModuleRouteListener();
08        $moduleRouteListener->attach($eventManager);
09        $eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR,array($this,'onDispatchError'),100);
10    }
11    function onDispatchError(MvcEvent $e) {
12         
13        $vm $e->getViewModel();
14        $vm->setTemplate('error/layout');
15         
16    }
17}
这个方式,解决由于路由问题导致的错误。

如果在controller > action 中进入错误页面,则非常简单,调用下面的方法:

1$this->notFoundAction();

0 0
原创粉丝点击