0

如何覆盖 Slim 的第 2 版默认错误处理?我不希望我的应用程序在每次收到警告消息时崩溃。基本上我想从 \Slim\Slim 类中覆盖函数 handleErrors() 。

我研究了如何覆盖这种行为,但因为它被称为:

set_error_handler(array('\Slim\Slim', 'handleErrors'));在 Sim 的 run() 方法中,我必须自己编辑 Slim 源代码。我将上面的内容更改为:set_error_handler(array(get_class($this), 'handleErrors'));然后我为 handleErrors() 使用不同的行为扩展了 Slim,并实例化了我的自定义类而不是 Slim。它工作正常但我不想触及 Slim 的核心课程。代码仅供参考

public static function handleErrors($errno, $errstr = '', $errfile = '', $errline = '')
{
    if (error_reporting() & $errno) {
        //Custom Block start here
        $search = 'Use of undefined constant';
        if(preg_match("/{$search}/i", $errstr)) {
            return true; //If undefined constant warning came will not throw exception
        }
        //Custom Block stop here
        throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
    }

    return true;
}

请帮助使用正确的方法来覆盖 handleErrors()

4

2 回答 2

0

我通过以下方式在我的 index.php 中处理它

// Prepare the Slim application container
$container = new Container();

// Set up error handlers
$container['errorHandler'] = function () {
    return new ErrorHandler();
};
$container['phpErrorHandler'] = function () {
    return new ErrorHandler();
};
$container['notAllowedHandler'] = function () {
    return new NotAllowedHandler();
};
$container['notFoundHandler'] = function () {
    return new NotFoundHandler();
};
// Set up the container
$app = new App($container);

$app->run();

也可以通过这种方式覆盖默认的 slim 请求和响应

于 2019-09-20T15:01:27.510 回答
0

扩展类并覆盖各自的方法 handleErrors() 和 run()。

class Core_Slim_Slim extends \Slim\Slim
{
public static function handleErrors($errno, $errstr = '', $errfile = '', $errline = '')
    {
        //Do whatever you want...
    }

public function run()
    {
        set_error_handler(array('Core_Slim_Slim', 'handleErrors'));
    }

}

实例化新的自定义 slim 类并调用它的 Run 方法,如下所示。

$app = new Core_Slim_Slim();
$app->run();

谢谢大家的回复:)

于 2019-09-23T08:02:14.510 回答