Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 315
Sasha Avatar asked Apr 13 '18 13:04

Sasha


1 Answers

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!

like image 190
Adam Kozlowski Avatar answered Nov 03 '22 00:11

Adam Kozlowski