我想完全禁止生产错误报告,因为我们有一些非常旧的代码,我们仍然需要修复,但现在确实有效(是的我也不喜欢它)。 我们无法在几天内修复所有内容,因此我们需要像往常一样压制警告和异常。
真正的问题是它已经在一个简单的懒惰错误上引发异常(因为没有定义var)
if(!$var) {
// do whatever
}
试着
APP_DEBUG = FALSE
APP_LOG_LEVEL =紧急
display_errors(false);
set_error_handler(null);
set_exception_handler(null);
但它仍然显示ErrorException
未定义的变量:script_name_vars_def
编辑 :代码的工作原理如下
web.php
Route::any('/someroute', 'somecontroller@controllerFunc');
somecontroller.php
public controllerFunc() {
ob_start();
require '/old_index.php';
$html = ob_get_clean();
return response($html);
}
这样我们就可以使用Laravel路由,而无需立即重写旧代码。
我知道我可以很容易地修复这个警告,但是这些错误还有很多,我们现在需要使用Laravel路由。 稍后解决问题。
思路
$dontReport
使用一些通配符。 @
suppress 编辑解释中间件无法正常工作的步骤
1)创建midddleware
php artisan make:middleware SuppressExceptions
2)写下来
SuppressExceptions.php
public function handle($request, Closure $next)
{
error_reporting(0);
return $next($request);
}
3)注册
laravel /应用/ HTTP / Kernel.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\SuppressExceptions::class,
],
是的,您可以更改错误报告。 实际上,框架提供了一个拦截异常的地方: App\\Exceptions\\Handler
。 默认情况下, render
方法会将抛出的异常转换为HTML响应。 APP_ENV
和APP_DEBUG
值只会改变此错误响应的呈现方式(基本上是否有异常堆栈跟踪的详细信息)。
尝试将render
方法更改为
public function render($request, Exception $exception)
{
if ($exception instanceof ErrorException) {
error_reporting(0);
$kernel = app(\Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle($request)->send();
return $kernel->terminate($request, $response);
}
return parent::render($request, $exception);
}
这基本上会关闭报告,然后尝试重新处理请求。 在if
子句中,您可以检查所需的任何条件(异常的类,严重性等)。 捕获ErrorException
可能会满足您的需求,但请注意您可能无法以这种方式从致命错误中恢复。
无论如何,你应该把它当作“概念证明”......对于非幂等请求,这种“重新处理”的方法并不好。 相反,只需创建一个中间件
public function handle($request, Closure $next)
{
error_reporting(0);
return $next($request);
}
与以前相同,致命错误无法通过这种方式恢复。 但是,您可以显示一个自定义错误消息,将此中间件与之前的异常处理程序方法相结合:
public function render($request, Exception $exception)
{
if ($exception instanceof FatalErrorException) {
return view('fatal-error', ['exception' => $exception]);
}
return parent::render($request, $exception);
}
我想你的php.ini
从另一个地方加载。 因此设置仍未应用。 尝试找到php.ini
正确位置(您可以在phpinfo()
查看信息)。 无论如何,您可以在index.php
使用您的参数重写这些参数:
error_reporting(0);
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
但正如@Davon在评论中所说。 这些设置将被Laravel覆盖。 因此,上面的代码可以放在您的控制器中。 但这将是肮脏的黑客。 所以你必须找到另一种方式。 尝试打印.env
的内容。 也许某些设置不正确。
error_reporting(0);
ini_set('display_errors', 0);
第二行更改php.ini
文件中'display_errors'的值
编辑:添加更多代码,以显示这是如何具体环境...
$ env = getenv('APPLICATION_ENV');
switch ($env) {
case 'production':
error_reporting(0);
$config = include __DIR__ . '/../app/config/config_prod.php';
break;
case 'staging':
ini_set('display_errors', 1);
$config = include __DIR__ . '/../app/config/config_staging.php';
break;
case 'development':
case 'local':
default:
ini_set('display_errors', 1);
$config = include __DIR__ . '/../app/config/config_local.php';
break;
Laravel调试设置位于.env
文件中,您可以在其中设置调试选项,如下所示:
APP_DEBUG = true
但...
Laravel还有配置机制,位于config
文件夹中的app.php
中,默认为:
'debug' => env('APP_DEBUG', false),
它告诉Laravel使用.env
值,默认为false
,但任何有权访问该文件的人都可以将其更改为:
'debug' => true,
这样你的.env
值就会被Laravel忽略。
如果问题是你看到'哎呀出错了'页面,你可以用@alepeino的答案解决这个问题:
https://stackoverflow.com/a/44862789/2777970
但是我将渲染方法更改为:
public function render($request, Exception $exception)
{
if (!config('app.debug')) {
error_reporting(0);
return response('nothing', 500);
}
return parent::render($request, $exception);
}
这个渲染方法(父)是构建并返回“Whoops”页面的html的方法,所以如果你覆盖它,你应该很酷。
要更改de debug config,请检查config / app.php是否具有使用ENV值APP_DEBUG的调试选项,并在生产.env上检查它是否设置为false(APP_DEBUG = false)。
Laravel强调错误和警告免费代码,因此处理此问题的最佳方法是确保您的代码不会产生任何错误,警告或通知。
更新:我不推荐以下方法用于最新版本的Laravel。 Laravel现在允许您更改非供应商类中的异常处理:App \\ Exceptions \\ Handler,如alepeino的答案所示。 中间件也可以是禁用error_reporting的更好解决方案。
以前的答案是出于历史目的而维护的,但我不建议修改供应商文件。
但是,如果您仍然决定改变此行为,则需要查看通常位于vendor / laravel / framework / src / illuminate / Foundation / Bootstrap / HandleExceptions.php中的名为HandleExceptions.php的文件:
public function bootstrap(Application $app)
{
$this->app = $app;
error_reporting(-1); // change this line to your desired reporting level
set_error_handler([$this, 'handleError']);
set_exception_handler([$this, 'handleException']);
register_shutdown_function([$this, 'handleShutdown']);
if (! $app->environment('testing')) {
ini_set('display_errors', 'Off');
}
}
第32行,其中error_reporting当前设置为-1。 https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php
当然,通过修改此代码,您可能需要阻止laravel / framework的更新,或者您需要在每次更新时验证此文件。
更新此代码后,您需要重新编译您的类:
php artisan clear-compiled; php artisan optimize
我是怎么做的
app/providers/AppServiceProvider
在启动功能
public function boot()
{
//add error reporting level
error_reporting(0);
}