phplaravelexceptionlaravel-5.5laravel-exceptions

Laravel 5.5. App facade is not working in the Exception handler


I have such a simplified version of a class

class Handler extends ExceptionHandler
{
    protected $dontReport = [];

    public function report(Exception $exception)
    {
        $environment = \App::environment();
        //...
    }

    //...
}

And I receive PHP Fatal error: Uncaught Error: Class 'App' not found in .../app/Exceptions/Handler.php:37.

In other places of the app it works.

In config/app.php it was registered.

'aliases' => [
    'App' => Illuminate\Support\Facades\App::class,
    //...
]

Solution

  • The problem was in jeroennoten/laravel-adminlte package, config/adminlte.php file.

    Looks like in

    'menu' => [
        [
            'text' => 'API documentation',
            'url'  => request()->getSchemeAndHttpHost() . '/docs',
            'icon' => 'file-o',
        ],
    ],
    

    request()->getSchemeAndHttpHost() in calling from the console has caused an exception and at that moment something related to facades was not initialized (would be glad to hear what exactly), so my handler has triggered a second exception I've been catching.

    I have extracted menu creation to the provider, but I'm not sure, if it's a good solution.

    class AdminMenuProvider extends ServiceProvider
    {
         public function boot(Dispatcher $events)
         {
             $events->listen(BuildingMenu::class, function (BuildingMenu $event) {
                $event->menu->add([
                    'text' => 'API documentation',
                    'url'  => request()->getSchemeAndHttpHost() . '/docs',
                    'icon' => 'file-o',
                ]);
                //...
            }
         }
    }