php 禁用 Laravel 错误处理程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25438560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:50:10  来源:igfitidea点击:

Disable laravel error handler

phplaravelerror-handlinglaravel-4

提问by Petah

Is there anyway to disable the laravel error handler all together?

反正有没有一起禁用laravel错误处理程序?

I want to simply display standard PHP errors, notthe Whoops, looks like something went wrongerrors.

我想简单的显示标准的PHP错误没有Whoops, looks like something went wrong错误。

回答by Alan Storm

Not without majorly violating the principles of the framework (which I'll tell you how to do below, if you're still interested).

并非没有严重违反框架的原则(如果您仍然感兴趣,我将在下面告诉您如何做)。

There's a few things that make this difficult to accomplish. It's easy enough to unset the default error and exception handlers

有几件事使这很难完成。取消设置默认错误和异常处理程序很容易

set_error_handler(null);
set_exception_handler(null);

but that leaves you with two major hurdles.

但这会给您带来两个主要障碍。

The first is Laravel registers a shutdown handler as part of its bootstrapping, and this shutdown function will look for the last error, and if it was a fatal error, manually call the exception handling code. There's no easy way to un-register a shutdown function.

第一个是 Laravel 注册一个关闭处理程序作为其引导的一部分,这个关闭函数将查找最后一个错误,如果是致命错误,则手动调用异常处理代码。有没有简单的方法来注销关机功能

The second is, the main Laravel Application handler looks like this

第二个是,主要的 Laravel 应用程序处理程序看起来像这样

#File: vendor/laravel/framework/src/Illuminate/Foundation/Application.php
public function handle(SymfonyRequest $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
    try
    {
        $this->refreshRequest($request = Request::createFromBase($request));

        $this->boot();

        return $this->dispatch($request);
    }
    catch (\Exception $e)
    {
        if ($this->runningUnitTests()) throw $e;

        return $this['exception']->handleException($e);
    }
}

That is -- if you application code throws an exception, Laravel catches it here and manually calls the exception's handleExceptionmethod (which triggers the standard Laravel exception handling). There's no way to let PHP handle a fatal exception that happens in your application, Laravel blocks that from ever happening.

也就是说——如果您的应用程序代码抛出异常,Laravel 会在此处捕获它并手动调用异常的handleException方法(触发标准的 Laravel 异常处理)。没有办法让 PHP 处理应用程序中发生的致命异常,Laravel 会阻止这种情况发生。

The part where I tell you how to do what you want

我告诉你如何做你想做的部分

All this means we need to replace the main Laravel application with our own. In bootstrap/start.php, there's the following line

所有这一切意味着我们需要用我们自己的 Laravel 应用程序替换主应用程序。在bootstrap/start.php,有以下行

#File: bootstrap/start.php
$app = new Illuminate\Foundation\Application;

Replace it with the following

将其替换为以下内容

ini_set('display_errors','1');
class MyApplication extends Illuminate\Foundation\Application
{
    function startExceptionHandling()
    {
        //do nothing
    }

    public function handle(Symfony\Component\HttpFoundation\Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
    {
        $this->refreshRequest($request = Request::createFromBase($request));

        $this->boot();

        return $this->dispatch($request);
    }    
}

$app = new MyApplication;

The first thing we're doing is setting PHP's display errors ini to 1. This makes sure errors are output to the browser.

我们要做的第一件事是将 PHP 的显示错误 ini 设置为1. 这可确保将错误输出到浏览器。

Next, we're defining a new application class that extends the real application class.

接下来,我们将定义一个扩展真实应用程序类的新应用程序类。

Finally, we replace the real Laravel $appobject with an object instantiated by our class.

最后,我们用$app我们的类实例化的对象替换真正的 Laravel对象。

In our application class itself we blank out startExceptionHandling. This prevents Laravel from setting up custom exception, error, and shutdown callbacks. We also define handleto remove the application boot/dispatch from a try/catch. This is the most fragile part of the process, and may look different depending on your Laravel version.

在我们的应用程序类本身中,我们将startExceptionHandling. 这可以防止 Laravel 设置自定义异常、错误和关闭回调。我们还定义handle从 try/catch 中删除应用程序启动/调度。这是整个过程中最脆弱的部分,根据你的 Laravel 版本可能看起来会有所不同。

Final Warnings

最后警告

If the handlemethod changes in future version of Laravel this will break.

如果该handle方法在 Laravel 的未来版本中发生变化,这将会中断。

If custom packages rely on adding custom exception handlers, they may break.

如果自定义包依赖于添加自定义异常处理程序,它们可能会中断。

I'd recommend staying away from this as anything other than a temporary debugging technique.

除了临时调试技术之外,我建议远离它。

回答by Komal

Then set 'debug' => false, in \config\local\app.phpfile

然后在\config\local\app.php文件中设置'debug' => false

<?php

return array(

    'debug' => false,

);

回答by rendra

In laravel 5 to disable debug, you just need to comment

在laravel 5中禁用调试,你只需要评论

//'debug' => env('APP_DEBUG'),

in \config\app.php file

在 \config\app.php 文件中

回答by Andreas

Exception handling is hardcoded into the Applicationclass. You can override the classes in your bootstrap/start.phpfile:

异常处理被硬编码到Application类中。您可以覆盖bootstrap/start.php文件中的类:

class ExceptionHandler {
    public function handleException($exception) {
        throw $exception;
    }
    public function handleConsole($exception) {
        throw $exception;
    }
}

class MyApplication extends Illuminate\Foundation\Application
{
    public function registerExceptionProvider() {}
    public function startExceptionHandling() {}
}

$app = new MyApplication;

It should go without saying that this is definitely not encouraged.

不用说,这绝对是不鼓励的。

回答by MacroMan

Related to the question: Laravel has a built in method for disabling exceptions in unit tests:

与问题相关:Laravel 有一个内置方法用于在单元测试中禁用异常:

$this->withoutExceptionHandling()

$this->withoutExceptionHandling()

Laracast on the subject: https://laracasts.com/series/whats-new-in-laravel-5-5/episodes/17

Laracast 关于这个主题:https://laracasts.com/series/whats-new-in-laravel-5-5/episodes/17

回答by kfriend

This will get you close. There might be a more proper way of doing this. It replaces Laravel's current exception handler with a single-method class. I haven't tested this, besides the basic route below, so you may need to add other methods to satisfy different situations.

这会让你接近。可能有更合适的方法来做到这一点。它用单方法类替换了 Laravel 当前的异常处理程序。这个我没有测试过,除了下面的基本路线,所以你可能需要添加其他方法来满足不同的情况。

class ExceptionHandler
{
    public function handleException($e)
    {
        echo $e;
        die;
    }
}

App::bind('exception', App::share(function($app)
{
    return new ExceptionHandler;
}));


Route::get('/', function()
{
    throw new Exception('Testing');
});

回答by Junior Laravel

In file .env, just change:

在 file 中.env,只需更改:

APP_DEBUG=true

To:

到:

APP_DEBUG=false