onl*_*mas 13 php laravel laravel-5 laravel-5.3 laravel-5.4
我想完全禁止生产错误报告,因为我们有一些非常旧的代码,我们仍然需要修复,但现在确实有效(是的我也不喜欢它).我们无法在几天内修复所有内容,因此我们需要像往常一样压制警告和异常.
真正的问题是它已经在一个简单的懒惰的bug上引发异常(因为没有定义var)
if(!$var) {
// do whatever
}
Run Code Online (Sandbox Code Playgroud)
试着
APP_DEBUG = FALSE
APP_LOG_LEVEL =紧急
display_errors(false);
set_error_handler(null);
set_exception_handler(null);
Run Code Online (Sandbox Code Playgroud)
但它仍然显示出一个 ErrorException
未定义的变量:script_name_vars_def
编辑:代码的工作原理如下
web.php
Route::any('/someroute', 'somecontroller@controllerFunc');
Run Code Online (Sandbox Code Playgroud)
somecontroller.php
public controllerFunc() {
ob_start();
require '/old_index.php';
$html = ob_get_clean();
return response($html);
}
Run Code Online (Sandbox Code Playgroud)
这样我们就可以使用Laravel路由,而无需立即重写旧代码.
我知道我可以很容易地修复这个警告,但是这些错误还有很多,我们现在需要使用Laravel路由.稍后解决问题.
思路
$dontReport.@在正确的地方使用抑制编辑解释中间件无法正常工作的步骤
1)创建midddleware
php artisan make:middleware SuppressExceptions
Run Code Online (Sandbox Code Playgroud)
2)写下来
SuppressExceptions.php
public function handle($request, Closure $next)
{
error_reporting(0);
return $next($request);
}
Run Code Online (Sandbox Code Playgroud)
3)注册
laravel /应用/ HTTP/Kernel.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\SuppressExceptions::class,
],
Run Code Online (Sandbox Code Playgroud)
am0*_*mhz 13
Laravel 调试设置位于.env文件中,您可以在其中设置调试选项,如下所示:
APP_DEBUG = true
Run Code Online (Sandbox Code Playgroud)
但...
Laravel 也有位于文件夹app.php中的配置机制,config默认为:
'debug' => env('APP_DEBUG', false),
Run Code Online (Sandbox Code Playgroud)
它告诉 Laravel 使用该.env值,并默认为false,但任何有权访问该文件的人都可以简单地将其更改为:
'debug' => true,
Run Code Online (Sandbox Code Playgroud)
这样你的.env价值就会被 Laravel 忽略。
是的,您可以更改错误报告.实际上,框架提供了拦截异常的地方: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);
}
Run Code Online (Sandbox Code Playgroud)
这基本上会关闭报告,然后尝试重新处理请求.在该if子句中,您可以检查您想要的任何条件(异常的类别,严重性等).捕获ErrorException可能会满足您的需求,但请注意,您可能无法以这种方式从致命错误中恢复.
无论如何,你应该把它当作"概念证明"......对于非幂等请求,这种"重新处理"的方法并不好.相反,只是创建一个中间件与
public function handle($request, Closure $next)
{
error_reporting(0);
return $next($request);
}
Run Code Online (Sandbox Code Playgroud)
与以前相同,致命错误无法通过这种方式恢复.但是,您可以显示一个自定义错误消息,将此中间件与之前的异常处理程序方法相结合:
public function render($request, Exception $exception)
{
if ($exception instanceof FatalErrorException) {
return view('fatal-error', ['exception' => $exception]);
}
return parent::render($request, $exception);
}
Run Code Online (Sandbox Code Playgroud)
我是怎么做的
app/providers/AppServiceProvider
Run Code Online (Sandbox Code Playgroud)
在启动功能
public function boot()
{
//add error reporting level
error_reporting(0);
}
Run Code Online (Sandbox Code Playgroud)
App Service Provider 在 Laravel 编译之前运行,因此在引导功能中您可以将错误报告设置为任何级别。它将为所有正在运行的脚本设置。您可以打开您的 app/providers/AppServiceProvider.php 并在启动功能中添加 error_reporting(0/1)
| 归档时间: |
|
| 查看次数: |
21168 次 |
| 最近记录: |