Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

php - Disable laravel error handler

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

I want to simply display standard PHP errors, not the Whoops, looks like something went wrong errors.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

#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 handleException method (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.

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

#File: bootstrap/start.php
$app = new IlluminateFoundationApplication;

Replace it with the following

ini_set('display_errors','1');
class MyApplication extends IlluminateFoundationApplication
{
    function startExceptionHandling()
    {
        //do nothing
    }

    public function handle(SymfonyComponentHttpFoundationRequest $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.

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

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

In our application class itself we blank out startExceptionHandling. This prevents Laravel from setting up custom exception, error, and shutdown callbacks. We also define handle to 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.

Final Warnings

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...