Laravel 5.6 - 未捕获的运行时异常 : A facade root has not been set

标签 laravel laravel-5

当我尝试在类里面使用 Illuminate\Http\Request 时,出现以下错误。

错误:

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  

相关类(class):

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',
        ];
    }

}

我在这里做错了什么?

最佳答案

您需要创建一个新的应用程序容器,然后将其绑定(bind)到 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);

以流明为单位: bootstrap/app.php

$app->withFacades();

祝你好运!

关于Laravel 5.6 - 未捕获的运行时异常 : A facade root has not been set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49818102/

相关文章:

php - 字符串中的 HTML 中断标记在浏览器中显示为 <br>,但也显示为 <br> 开发控制台

php - 迁移不要把 "unique"放在我的 table 上

php - Laravel 旧()不工作

Laravel 路由在实时服务器上不起作用

laravel-5 - 拉拉维尔 5 : Retrieve JSON array from $request

php - Nginx Laravel 配置返回 403

php - 如果倒数计时器在 Laravel 6 中过去了,我想更改数据值

php - Laravel 5.7 分页在更改页面时显示但不返回正确的数据

php - Laravel - 归档数据,删除和插入到另一个表

php - laravel 数据库查询 max(date) 获取所有帖子