Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我可以在Silex中禁用错误/异常处理吗?_Php_Symfony_Error Handling_Frameworks_Silex - Fatal编程技术网

Php 我可以在Silex中禁用错误/异常处理吗?

Php 我可以在Silex中禁用错误/异常处理吗?,php,symfony,error-handling,frameworks,silex,Php,Symfony,Error Handling,Frameworks,Silex,我正在基于Silex 1.3构建一个应用程序。这是我第一次接触Silex,所以我对它不太熟悉 我想使用我自己的错误/异常处理程序,它基本上是一个注册自身的类,然后将捕获所有错误、致命错误和未捕获的异常,并处理它们,无论是在开发中,还是在生产中,使用一个优雅的处理程序 然而,一旦我进入silex控制器、中间件,不管怎样,silex将接管并使用它自己的错误处理。我的仍然会捕获致命错误,因为Silex显然没有挂接到关机中,但其他所有内容都被Silex的默认“出错”页面替换 我知道我可以使用$app->

我正在基于Silex 1.3构建一个应用程序。这是我第一次接触Silex,所以我对它不太熟悉

我想使用我自己的错误/异常处理程序,它基本上是一个注册自身的类,然后将捕获所有错误、致命错误和未捕获的异常,并处理它们,无论是在开发中,还是在生产中,使用一个优雅的处理程序

然而,一旦我进入silex控制器、中间件,不管怎样,silex将接管并使用它自己的错误处理。我的仍然会捕获致命错误,因为Silex显然没有挂接到关机中,但其他所有内容都被Silex的默认“出错”页面替换

我知道我可以使用$app->error()来覆盖Silex处理错误的方式,但是我还没有找到一种方法将事情设置回原来的ErrorHandler,或者覆盖Silex是否处理错误

那么,有人知道如何a)告诉Silex使用我的错误处理程序,使用$app->error()或其他方式,b)完全禁用Silex中的错误处理,或者c)作为最后手段,让Silex捕获致命错误,以便我可以从$app->error()中处理所有三种类型吗?

由于这是我第一次使用Silex,如果有更好的方法,请随时纠正我或向我展示如何处理Silex中的错误,但如果可以,请回答问题。

一些示例代码:

// This will register itself and then handle all errors.
$handler = new ErrorHandler();

// These are all handled appropriately.
nonexistentfunction();            // Correctly caught by ErrorHandler::handleFatalError
trigger_error("example");         // Correctly caught by ErrorHandler::handlePhpError
throw new \Exception("example");  // Correctly caught by ErrorHandler::handleException

$app = new \Silex\Application();
$app->get('/', function () use ($app) {

    // This is still handled correctly.
    nonexistentfunction();            // Correctly caught by ErrorHandler::handleFatalError

    // However, these are now overridden by Silex.
    trigger_error("example");         // INCORRECTLY DISPLAYS SILEX ERROR PAGE.
    throw new \Exception("example");  // INCORRECTLY DISPLAYS SILEX ERROR PAGE.

});
$app->run();
还有一个非常简化的ErrorHandler供参考:

Class ErrorHandler
{
    public function __construct()
    {
        $this->register();
    }

    private function register()
    {
        register_shutdown_function( array($this, "handleFatalError") );
        set_error_handler(array($this, "handlePhpError"));
        set_exception_handler(array($this, "handleException"));
    }

    // Etc.

}

您必须在应用程序中注册特定提供商:

我知道选项
(b)
您可以完全禁用Silex应用程序错误处理程序,然后,您的自定义错误处理程序将按照您的定义正常工作

完全禁用的Silex错误处理程序:

$app['exception_handler']->disable();
因此,它将像:

require_once  'Exception.php'; # Load the class
$handler = new ErrorHandler(); # Initialize/Register it

$app = new \Silex\Application();
$app->get('/', function () use ($app) {


    nonexistentfunction();  
    trigger_error("example");
    throw new \Exception("example");

});
$app->run();

看起来,您还需要注册一个ExceptionHandler。Silex将致命错误转化为异常以处理它们。另外,如果我没记错的话,当在控制器和中间件(至少在中间件之前)中“抛出”时,这种异常会被捕获,但在模型中不会

最后,您可以添加以下内容来处理问题

// register generic error handler (this will catch all exceptions)
$app->error(function (\Exception $e, $exceptionCode) use ($app) {
    //if ($app['debug']) {
    //    return;
    //}

    return \Service\SomeHelper::someExceptionResponse($app, $e);
});

return $app;

我希望这会有所帮助。

请注意,ExceptionHandler::disable()已被删除。因此:

在2.0之前的Silex中:

$app['exception_handler']->disable();
在Silex 2.0+中:

unset($app['exception_handler']);

伙计,说真的。你读过这个问题吗?读这个问题,我的意思是,甚至只是读这个问题的标题?甚至只是标题。$app['exception_handler']->disable();谢谢这正是我需要的。有趣的是,现在你给了我,我可以在API中找到它。我希望他们的文档能更好一点(o)