简体   繁体   中英

Laravel 5.6 - Uncaught RuntimeException: A facade root has not been set

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?

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!

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.

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.

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. Eg 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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM