简体   繁体   English

设置Illuminate查询生成器-Uncaught RuntimeException:尚未设置外观根

[英]Setting up Illuminate query builder - Uncaught RuntimeException: A facade root has not been set

I am trying to set up Illuminate query builder, so I can run queries like "DB::table('table')->where(...)" .. however I can't get it to work. 我正在尝试设置Illuminate查询生成器,因此我可以运行“ DB :: table('table')-> where(...)”之类的查询..但是我无法使其正常工作。

I downloaded Laravel throught composer (require laravel/laravel). 我通过作曲家下载了Laravel(需要laravel / laravel)。 Next I have created a index.php, where I am including composer autoload file. 接下来,我创建了index.php,其中包含了作曲家自动加载文件。 After that, I am trying to call a simple query: 之后,我试图调用一个简单的查询:

\Illuminate\Support\Facades\DB::table('users')->get();

However it throws exception "Uncaught RuntimeException: A facade root has not been set.". 但是,它将引发异常“ Uncaught RuntimeException:尚未设置外观根”。 I was not expecting it to work right away, because I didn't specified the database connection. 我没想到它会立即生效,因为我没有指定数据库连接。 But based on the message of this exception I am not much clever. 但是根据有关此异常的消息,我不太聪明。

I found a solution here on SO to put this before using query builder: 我在SO上找到了一个解决方案,可以在使用查询生成器之前将其放入:

$Capsule = new Capsule;
$Capsule->addConnection(config::get('database'));
$Capsule->setAsGlobal();
$Capsule->bootEloquent();

However the config::get('database') statement throws the same exception too. 但是config::get('database')语句也会引发相同的异常。 Which means I probably have to configure the config somehow too. 这意味着我可能也必须以某种方式配置配置。

I have tried to include Laravel's bootstrap and boot it, but it does not change anything. 我试图包括Laravel的引导程序并引导它,但是它没有任何改变。

$app = require_once '\composer\vendor\laravel\laravel\bootstrap\app.php';
$app->boot();

Then I tried to set Config's facade app by this statement: Config::setFacadeApplication($app) After that, when I try to call Config::get('database'), it throws other exception Uncaught ReflectionException: Class config does not exist 然后,我尝试通过以下语句设置Config的Facade应用程序: Config::setFacadeApplication($app)之后,当我尝试调用Config :: get('database')时,它将引发其他异常Uncaught ReflectionException: Class config does not exist

Now I am out of ideas how to get it working. 现在我不知道如何使它工作。 What am I missing here? 我在这里想念什么?

Solved by extending an Application class and defining config and db instances in bootstrap function, like this: 通过扩展Application类并在bootstrap函数中定义config和db实例来解决,如下所示:

use Illuminate\Config\Repository;
use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Database\DatabaseManager;
use Illuminate\Foundation\Application;

class LaravelApp extends Application
{

    function boot()
    {
        parent::boot();

        $this->instance('config', new Repository(['database' => $this->getDBCfg()]));
        $this->instance('db', new DatabaseManager($this, new ConnectionFactory($this)));
    }

    private function getDBCfg(){
        return [

            'default' => env('DB_CONNECTION', 'mysql'),

            'connections' => [

                'mysql' => [
                    'driver' => 'mysql',
                    'host' => env('DB_HOST', 'localhost'),
                    'port' => env('DB_PORT', '3306'),
                    'database' => env('DB_DATABASE', 'test'),
                    'username' => env('DB_USERNAME', 'root'),
                    'password' => env('DB_PASSWORD', ''),
                    'unix_socket' => env('DB_SOCKET', ''),
                    'charset' => 'utf8mb4',
                    'collation' => 'utf8mb4_unicode_ci',
                    'prefix' => '',
                    'strict' => true,
                    'engine' => null,
                ],

            ],

        ];
    }
}

Client code looks like this: 客户端代码如下所示:

$laravelApp = new LaravelApp(__DIR__);
$laravelApp->boot();
Config::setFacadeApplication($laravelApp);
var_dump(\Illuminate\Support\Facades\DB::table('test')->get());

Thanks for help in comments 感谢您的评论帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:未捕获RuntimeException:尚未设置外观根 - Fatal error: Uncaught RuntimeException: A facade root has not been set Laravel 5.6 Uncaught RuntimeException:尚未设置外观根。 在vendor / laravel / framework / src / Illuminate / Support / Facades / Facade.php:218中 - Laravel 5.6 Uncaught RuntimeException: A facade root has not been set. in vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:218 未捕获的 RuntimeException:尚未设置外观根。 在 Facade.php:258 升级到 Laravel 时 - Uncaught RuntimeException: A facade root has not been set. in Facade.php:258 when upgrading to Laravel 7 “RuntimeException:尚未设置立面根” - "RuntimeException: a facade root has not been set" 获取 RuntimeException:尚未设置外观根。 在 /var/www/html/vendor/.../Illuminate/Support/Facades/Facade.php - Getting RuntimeException: A facade root has not been set. in /var/www/html/vendor/.../Illuminate/Support/Facades/Facade.php 将 Laravel 从 5.7 更新到 5.8 会导致此错误:致命错误:未捕获的运行时异常:尚未设置外观根 - updating Laravel from 5.7 to 5.8 results in this error: Fatal error: Uncaught RuntimeException: A facade root has not been set 在共享主机上使用 Laravel 项目部署错误(未捕获的 RuntimeException:尚未设置外观根) - Deploy error with Laravel project on Shared Hosting (Uncaught RuntimeException: A facade root has not been set) Laravel php artisan config:cache抛出Uncaught RuntimeException:尚未设置外观根。 安装新的作曲家程序包后 - Laravel php artisan config:cache throws Uncaught RuntimeException: A facade root has not been set. After installing new composer package 尚未设置外观根。 流明 - A facade root has not been set. in lumen Laravel - 尚未设置外观根 - Laravel - A facade root has not been set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM