0

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,
    //...
]
D.R.
  • 2,540
  • 3
  • 24
  • 49

2 Answers2

0

Add to top of your class use App then you can use App Facade like App::environment()

Saad
  • 1,155
  • 3
  • 16
  • 36
  • The problem is that I can't use this facade at all. Both `\App` or `App` with `use`. I've used `\App` version in the example for the simplicity's sake :) – D.R. Oct 10 '17 at 05:41
  • Have you tried importing the full path of App `use Illuminate\Support\Facades\App`? Does that work? – Saad Oct 10 '17 at 05:44
  • Yes, I've also tried it too. I decided, that it's a very strange error, so it deserves its own question on SO :) – D.R. Oct 10 '17 at 05:45
  • 1
    Interesting it works on my `ExceptionHandler` is there a problem in your namespace? – Saad Oct 10 '17 at 05:53
  • No, it's not :) I have found the problem and created an answer. You can check it, I think it was a quite interesting problem :) – D.R. Oct 10 '17 at 06:05
0

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',
            ]);
            //...
        }
     }
}
D.R.
  • 2,540
  • 3
  • 24
  • 49