设置Illuminate查询生成器-Uncaught RuntimeException:尚未设置外观根 [英] Setting up Illuminate query builder - Uncaught RuntimeException: A facade root has not been set
问题描述
我正在尝试设置Illuminate查询生成器,因此我可以运行"DB :: table('table')-> where(...)"之类的查询..但是我无法使其正常工作.
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.
我通过作曲家下载了Laravel(需要laravel/laravel).接下来,我创建了一个index.php,其中包含了作曲家自动加载文件.之后,我尝试调用一个简单的查询:
I downloaded Laravel throught composer (require laravel/laravel). Next I have created a index.php, where I am including composer autoload file. After that, I am trying to call a simple query:
\Illuminate\Support\Facades\DB::table('users')->get();
但是它将引发异常未捕获的RuntimeException:尚未设置外观根.".我没想到它会立即生效,因为我没有指定数据库连接.但是根据这个异常的消息,我并不聪明.
However it throws exception "Uncaught RuntimeException: A facade root has not been set.". 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.
我在SO上找到了一个解决方案,可以在使用查询生成器之前将其放到这里:
I found a solution here on SO to put this before using query builder:
$Capsule = new Capsule;
$Capsule->addConnection(config::get('database'));
$Capsule->setAsGlobal();
$Capsule->bootEloquent();
但是 config :: get('database')
语句也会引发相同的异常.这意味着我可能也必须以某种方式配置配置.
However the config::get('database')
statement throws the same exception too. Which means I probably have to configure the config somehow too.
我尝试包括Laravel的引导程序并引导它,但是它没有任何改变.
I have tried to include Laravel's bootstrap and boot it, but it does not change anything.
$app = require_once '\composer\vendor\laravel\laravel\bootstrap\app.php';
$app->boot();
然后我尝试通过以下语句设置Config的Facade应用程序: Config :: setFacadeApplication($ app)
之后,当我尝试调用Config :: get('database')时,它将抛出其他异常未捕获的ReflectionException:类配置不存在
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
现在我不知道如何使它工作.我在这里想念什么?
Now I am out of ideas how to get it working. What am I missing here?
推荐答案
通过扩展Application类并在bootstrap函数中定义config和db实例来解决,例如:
Solved by extending an Application class and defining config and db instances in bootstrap function, like this:
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,
],
],
];
}
}
客户端代码如下:
$laravelApp = new LaravelApp(__DIR__);
$laravelApp->boot();
Config::setFacadeApplication($laravelApp);
var_dump(\Illuminate\Support\Facades\DB::table('test')->get());
感谢评论中的帮助
这篇关于设置Illuminate查询生成器-Uncaught RuntimeException:尚未设置外观根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!