雄辩的致命错误:参数传递的不是正确的实例

时间:2014-08-13 15:01:52

标签: php eloquent slim

我正在使用Slim和Eloquent在PHP中构建端点系统,如here所述。在我的本地开发中运行它时,下面的代码失败,根据方法的预期显示为致命错误

// Load Eloquent
$connFactory = new \Illuminate\Database\Connectors\ConnectionFactory();
$conn = $connFactory->make($settings);
$resolver = new \Illuminate\Database\ConnectionResolver();
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
\Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);

实际错误是:

[Wed Aug 13 10:31:44 2014] PHP Catchable fatal error:  Argument 1 passed to
Illuminate\Database\Connectors\ConnectionFactory::__construct() must be an instance 
of Illuminate\Container\Container, none given, called in
/Users/outsider/application/index.php on line 22 and defined in
/Users/outsider/application/vendor/illuminate/database/Illuminate/Database/Connectors/ConnectionFactory.php on line 25

关于此问题的文档中没有很多指南。关于可能原因的任何想法?

2 个答案:

答案 0 :(得分:5)

感谢Manolo指出我错过了什么。我需要声明一个Container并对其进行初始化:

$container = new Illuminate\Container\Container;
$connFactory = new \Illuminate\Database\Connectors\ConnectionFactory($container);
$conn = $connFactory->make($settings);
$resolver = new \Illuminate\Database\ConnectionResolver();
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
\Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);

答案 1 :(得分:5)

在Laravel外面使用Eloquent有一种更简单的方法。您可以使用Capsule:

/* Setup Eloquent. */
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;

$capsule = new Capsule;
$capsule->addConnection([
    "driver"    => "mysql",
    "host"      => "localhost",
    "database"  => "example",
    "username"  => "root",
    "password"  => "t00r",
    "charset"   => "utf8",
    "collation" => "utf8_general_ci",
    "prefix"    => ""
]);

$capsule->setEventDispatcher(new Dispatcher(new Container));
$capsule->bootEloquent();