我正在尝试在我正在构建的自定义PHP框架中通过作曲家使用Laravel的Eloquent ORM,并且在我的控制器中调用create方法时出现了一个奇怪的错误。
错误:
致命错误:未捕获的异常' Illuminate \ Contracts \ Container \ BindingResolutionException'消息'目标[Illuminate \ Contracts \ Debug \ ExceptionHandler]不可实例化。' in /Users/justin/Sites/test/vendor/illuminate/container/Container.php:763 Stack trace:#0 /Users/justin/Sites/test/vendor/illuminate/container/Container.php(644):Insuminate \ Container \ Container-> build(' Illuminate \ Cont ...',Array)#1 /Users/justin/Sites/test/vendor/illuminate/database/Connectors/ConnectionFactory.php(130) :Illuminate \ Container \ Container-> make(' Illuminate \ Cont ...')#2 [内部功能]:Illuminate \ Database \ Connectors \ ConnectionFactory-> Illuminate \ Database \ Connectors {closure }()#Users/justin/Sites/test/vendor/illuminate/database/Connection.php(964):call_user_func(Object(Closure))#4 / Users / justin / Sites / test / vendor / illuminate / database /Connection.php(832):Illuminate \ Database \ Connection-> getPdo()#5 /Users/justin/Sites/test/vendor/illuminate/database/Connection.php(717):Illuminate \ Database \ Connection-&gt ;在763行的/Users/justin/Sites/test/vendor/illuminate/container/Container.php中记录
数据库连接:
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'database',
'port' => '3306',
'charset' => 'utf-8',
'collation' => 'utf8_unicode_ci'
]);
$capsule->bootEloquent();
型号:
use Illuminate\Database\Eloquent\Model as Eloquent;
class User extends Eloquent{
protected $fillable = ['username','email'];
}
最后方法:
public function create($username = '', $email = ''){
User::create([
'username' => $username,
'email' => $email
]);
}
有人能帮助我吗?
答案 0 :(得分:1)
更新:已实现我使用的是错误版本的Illuminate,需要使用4.2
答案 1 :(得分:1)
您的Eloquent初始化可能不完整。
以下是我在Silex应用程序中实现它的方法:
<?php
namespace App\Base\Provider\Database;
use Silex\Application;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager as Capsule;
class DatabaseSummoner
{
public static function setup(Application $app)
{
$app['db'] = $app->share(function () use ($app) {
# Set up Fluent Query Builder
$capsule = new Capsule;
$db_settings = $app['config']['db'];
$capsule->addConnection(
$db_settings['connections'][$db_settings['connection']]
);
return $capsule;
});
// Set the event dispatcher used by Eloquent models...
$app['db']->setEventDispatcher(new Dispatcher(new Container));
// These two have to be outside closure
// Make the Capsule instance available globally via static methods...
$app['db']->setAsGlobal();
// Boot Eloquent ORM
$app['db']->bootEloquent();
}
}
然后在我的composer.json中,我需要"illuminate/database": "~5.3"
和"illuminate/events": "~5.3"
(这第二个是可选的,但如果你想使用模型事件,则很有用。)
最后,在__construct()
课程的Application
中,我致电DatabaseSummoner::setup($this);
。
这明显不同于您的设置,但您可能会找到自己的方式。