如何覆盖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的核心课程。
代码FYI
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()
答案 0 :(得分: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();
也可以通过这种方式覆盖默认的苗条请求和响应
答案 1 :(得分: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'));
}
}
实例化新的自定义苗条类,并将其Run方法称为波纹管。
$app = new Core_Slim_Slim();
$app->run();
感谢大家的回复:)