Zend 2:如何使用自定义控制器配置错误?

时间:2013-06-17 20:14:35

标签: php model-view-controller zend-framework2

我的问题是关于定制如何在Zend 2中处理错误。

假设我想自定义布局,以便在控制器的操作中执行此操作:

$layout = $this->layout();
$myNav = new ViewModel(array('nav' => $this->getNav());
$myNav->setTemplate('layout/nav');
$layout->addChild($myNav, 'navigation');

当我将其放入我的控制器以进行常规(即非404)观看时,效果很好。现在我已经自定义了我的布局,以便我可以<?php echo $this->navigation; ?>并且layout/nav.phtml被激活,所有内容都只是笨拙的。

现在,假设我想在呈现错误时做同样的事情。我需要能够在错误处理程序将其自己的ViewModel(...)返回到error/404.phtml模板之前以某种方式注入上述代码。

你是怎么做到的?

我怀疑这就像在module.config.php中为服务管理器设置正确的类一样:

'service_manager' => array(
    'services' => array(
        'error_handler' => 'MyModule\Controller\MyCustomErrorController'
        //and so on...

我该怎么做?

更新:

在我的Module.php我附加了MvcEvent::EVENT_DISPATCH_ERROR的方法。变体A起作用,变体B起作用。所以你不能在这里使用部分?我错过了一些非常基本的东西吗?

变体A

public function onDispatchError(MvcEvent $event)
{
    $sm  = $event->getApplication()->getServiceManager();
    $vm = $event->getViewModel();
    $vm->setVariable('nav', '<h1>test do i work?</h1>');
    //Works
}

变体B

public function onDispatchError(MvcEvent $event)
{
    $sm  = $event->getApplication()->getServiceManager();
    $vm = $event->getViewModel();
    $nav = new ViewModel(array('test'=>'hello there'));
    $nav->setTemplate('layout/simpletest');//contents: <?php echo $this->test; ?>
    $vm->addChild($nav, 'nav');
    //In the template, <?php echo $this->nav; ?> has nothing...
}

2 个答案:

答案 0 :(得分:1)

Zf2使用module.config.php文件来设置错误处理:

'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',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),

这应该处理4xx客户端错误和5xx服务器错误。

对于特定模块中的自定义错误页面。

namespace ModuleName;

use Zend\ModuleManager\Feature\BootstrapListenerInterface;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\Mvc\MvcEvent;

class Module implements
    BootstrapListenerInterface,
    AutoloaderProviderInterface,
    ConfigProviderInterface
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $eventManager->attach('dispatch', array($this, 'loadConfiguration' ), 100);
    }

    public function loadConfiguration(MvcEvent $e)
    {
    $sm  = $e->getApplication()->getServiceManager();

        $controller = $e->getRouteMatch()->getParam('controller');
        if (0 !== strpos($controller, __NAMESPACE__, 0)) {
            //if not this module
            return;
        }

        //if this module
    $exceptionstrategy = $sm->get('ViewManager')->getExceptionStrategy();
    $exceptionstrategy->setExceptionTemplate('error/errorcustom');
    }

    public function getAutoloaderConfig(){ /* common code */ }
    public function getConfig(){ /* common code */}
}

解决方案由http://samsonasik.wordpress.com/2012/09/19/zend-framework-2-create-custom-error-page/

的“samsonasik”提供

答案 1 :(得分:0)

你可以附加一个偶数来处理404被触发时会发生什么: Module.php

public function onBootstrap(MvcEvent $e)
{
    $eventManager = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
    /**
     * Log any Uncaught Errors
     */
    $sharedManager = $e->getApplication()->getEventManager()->getSharedManager();
    $sm = $e->getApplication()->getServiceManager();
    $sharedManager->attach('Zend\Mvc\Application', 'dispatch.error',
         function($e) use ($sm) {
            /**
             * Decide what to do now we've got a problem...
             * Log the issue etc..
             * You could forward to some custom controller if you wanted..
             */
            //$sm->get('Zend\Log\Logger')->crit('an error occurred... bla');
            $controller = $e->getTarget();
            //$routeMatch = $e->getRouteMatch();
            $controller->layout('somelayout'); // possibly change the layout..
         }
    );
}