7

I am getting following error when I try to use Illuminate\Http\Request in my class.

Error:

PHP Fatal error:  Uncaught RuntimeException: A facade root has not been set. in /home/sasha/Documents/OffProjects/vetnearme/vetnearme/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:218
Stack trace:
#0 /home/sasha/Documents/OffProjects/vetnearme/vetnearme/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(396): Illuminate\Support\Facades\Facade::__callStatic('replaceNamespac...', Array)
#1 /home/sasha/Documents/OffProjects/vetnearme/vetnearme/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(373): Illuminate\Foundation\Exceptions\Handler->registerErrorViewPaths()
#2 /home/sasha/Documents/OffProjects/vetnearme/vetnearme/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(288): Illuminate\Foundation\Exceptions\Handler->renderHttpException(Object(Symfony\Component\HttpKernel\Exception\HttpException))
#3 /home/sasha/Documents/OffProjects/vetnearme/vetnearme/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(187): Illumina in /home/sasha/Documents/OffProjects/vetnearme/vetnearme/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 218  

The class in question:

namespace App\App\Components;

use Illuminate\Http\Request;

/**
 * This class will be used to build menu for admin panel based on the user role
 */
class AdminPanelMenu {

    static function menu(Request $request){

        $user = $request->user();

        if($user->hasRole['super_admin'])
            return self::superAdmin();

        if($user->hasRole['admin'])
            return self::admin();

        if($user->hasRole['user'])
            return self::user();

        return [];

    }

    private static function superAdmin()
    {
        return [
            'MAIN NAVIGATION',
        ];
    }

    private static function admin()
    {
        return [
            'MAIN NAVIGATION',
        ];
    }

    private static function user()
    {
        return [
            'MAIN NAVIGATION',
        ];
    }

}

What am I doing wrong here?

Sasha
  • 8,521
  • 23
  • 91
  • 174
  • I am just adding a solution for those who are facing same issue but not able to solve it even after making changes as suggested in accepted answer. This can be an issue due to SELinux in action. Try disabling SELinux and if that works, you need to change context using `$sudo chcon -t httpd_sys_rw_content_t /path/to/your/laravel/project/dir -R`. I managed to solve my problem like this. – FMoridhara May 18 '18 at 10:14

4 Answers4

7

You need to create a new app container and then bind it to the Facade.

use \Illuminate\Container\Container as Container;
use \Illuminate\Support\Facades\Facade as Facade;

/**
* Setup a new app instance container
* 
* @var Illuminate\Container\Container
*/
$app = new Container();
$app->singleton('app', 'Illuminate\Container\Container');

/**
* Set $app as FacadeApplication handler
*/
Facade::setFacadeApplication($app);

in lumen: bootstrap/app.php

$app->withFacades();

Good luck!

Adam Kozlowski
  • 5,606
  • 2
  • 32
  • 51
  • 5
    So, I am lost :D. Where I need to make new app container? – Sasha Apr 13 '18 at 13:55
  • @Adam Kozlowski I'm facing same error but on different place (Uncaught RuntimeException: A facade root has not been set. in vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:218) after fresh installation of Laravel 5.6 on php 7.1.8. I installed it using composer and not faced any problem while installation. I have provided enough permissions to directories as instructed on installation guide and host is configured properly as well. Can you please help me if I can solve it in the same way you suggested? Facade concept is very new for me. – FMoridhara May 14 '18 at 07:07
  • Also lost, still looking for the location of the app container – Martijn Imhoff Aug 22 '18 at 13:07
  • Try to do this in bootstrap/app.php then clear cache – Adam Kozlowski Aug 22 '18 at 13:17
  • `bootstrap/app.php` under Laravel 5.3 (I know, out-dated) already has a `$app` instance: `Illuminate\Foundation\Application` is being used. I'm getting this with the `Log` facade. Edit: 5.6 is mentioned, okay. – Roland Oct 08 '18 at 15:45
  • After doing this, I am getting another error now, `Class view does not exist`. Foound out on another place that it's possible that my newest version of homestead is the problem. – Mladen Janjetovic Nov 13 '20 at 09:34
3

I know this is old but maybe it will help someone. I ran into this problem after I was fiddling with my app/config.php file. I added some options and accidentally put a semi-colon instead of a comma after it. I had:

'vapid_public_key'   => env('VAPID_PUBLIC_KEY'); <--- offending semi-colon
'vapid_private_key'  => env('VAPID_PRIVATE_KEY'),

Changed it to the proper comma and everything works as expected.

writeNow
  • 49
  • 1
  • 6
  • Thank you! My offence was defining a config property like this: 'readings_key' = env('WATER_READINGS_KEY') // should be '=>' – Roark Jun 18 '20 at 06:24
0

I think the problem is that Laravel is confusing scope resolution to facade error. check your code so make sure you do not have any static variable not existing in a class. E.g if you have a PHP class like;

<?php 

  class StaticExample 
  {
    public const EXAM = 'exam';
  }

  ?>

then you try to call a non-existing const StaticExample::EXAMS. Laravel will give you the above error which makes no sense because it's very difficult to trace. No error in the logs and you're lost.

My solution is to use an editor like PHPStorm that will point out your development errors. Another way is that you should check your scope resolutions very well.

Olotin Temitope
  • 419
  • 6
  • 13
-1

Really late but hopefully can help someone else. I found the easiest solution to this error was just to change the route (ie from /post to /posts) of the page that this error appears. And don't forget to change anywhere that has direct links to it.

Dharman
  • 30,962
  • 25
  • 85
  • 135
staff_sgn
  • 11
  • 5