我正在关注此README,并想知道是否有可能使以下内容起作用:
$capsule = new Illuminate\Database\Capsule\Manager;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
# can Facades be used like this somehow?
var_dump(DB::connection()->getPdo());
var_dump(Hash::make('hash'));
我收到以下预期的错误,因为脚本未加载任何Facades,但是我不确定如何调整工作方式>
Fatal error: Uncaught Error: Class 'DB' not found
我也尝试了接受的答案here,但仍然没有运气(甚至不确定我是否正确使用了它?)。.
$container = new Illuminate\Container\Container();
Illuminate\Support\Facades\Facade::setFacadeApplication($container);
$container->singleton('db', 'Illuminate\Support\Facades\DB');
class_alias('Illuminate\Support\Facades\DB', 'DB');
这给了我以下错误:
Fatal error: Uncaught Error: Call to undefined method Illuminate\Support\Facades\DB::connection()
添加“使用数据库;”仍然出现以下错误:
Fatal error: Uncaught Error: Class 'DB' not found
添加“使用Illuminate \ Support \ Facades \ DB;”顶部会出现以下错误:
Fatal error: Uncaught RuntimeException: A facade root has not been set.
有什么建议吗?还是不能仅将Facades与Illuminate Database一起使用?
答案 0 :(得分:1)
好吧,我终于弄明白了。
composer.json
{
"require": {
"illuminate/database": "^5.7",
"illuminate/hashing": "^5.7"
}
}
bootstrap.php
require 'vendor/autoload.php';
$app = new Illuminate\Container\Container();
Illuminate\Support\Facades\Facade::setFacadeApplication($app);
$app->singleton('db', function () use ($app) {
$capsule = new Illuminate\Database\Capsule\Manager;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
return $capsule;
});
$app->singleton('hash', function () use ($app) {
return new Illuminate\Hashing\HashManager($app);
});
class_alias(Illuminate\Support\Facades\DB::class, 'DB');
class_alias(Illuminate\Support\Facades\Hash::class, 'Hash');
index.php
require '../bootstrap.php';
var_dump(DB::connection()->getPdo());
var_dump(Hash::make('password'));
# object(PDO)#16 (0) {}
# string(60) "$2y$10$aevTHr94mYoh9PBlJz43EuT2qJLCKkNrOLNpKLhbjGzlqpMS6YVBG"
答案 1 :(得分:0)