繁体   English   中英

Zend Framework 3 中处理内部服务器错误、Mysql 错误(错误处理)的完美方法

[英]Perfect Way to Handle Internal server error,Mysql errors(Error handling) in Zend Framework 3

我想在 zend 框架中进行异常处理。我需要提供带有状态码的友好错误作为在发生错误时的响应。 这是我的模块。php 我只收到 controller 路由错误,但在 catch 块中没有其他任何内容,我在 module.php 中添加了这个仅一个特定模块

<?php
/**
 * @license   http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
 * @copyright Copyright (c) 2014-2016 Zend Technologies USA Inc. (http://www.zend.com)
 */

namespace Application;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\ModuleRouteListener;
use Application\Authorization\AuthorizationListener;
use ZF\MvcAuth\MvcAuthEvent;

class Module
{
    public function getConfig()
    {
        return include __DIR__ . '/../config/module.config.php';
    }

    public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);


            $application = $e->getApplication();
    $config = $application->getConfig();
    $eventManager = $application->getEventManager();
    $eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, [$this, 'catchError']);
    $eventManager->attach(MvcEvent::EVENT_RENDER_ERROR, [$this, 'catchError']);


    }

    public function handleError(MvcEvent $e)
{

    //get the exception
    $exception = $e->getParam('exception');
    //...handle the exception... maybe log it and redirect to another page, 
    //or send an email that an exception occurred...
}

public function catchError(MvcEvent $event) {
    $exception = $event->getParam('exception');
    echo "Exception: ".$exception;
  }   


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM