I did a couple of projects with Laravel in the past but now I need something very light for a project and went to use Slim, it works great for what I need and I want the great Eloquent ORM and Query Builder of Laravel, can't go without it now :) Now I manage to get it all working with composer, using the information that Taylor displayed on his GitHub, copied his piece of code
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
// Set the event dispatcher used by Eloquent models... (optional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));
// Set the cache manager instance used by connections... (optional)
$capsule->setCacheManager(...);
// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();
That works perfectly good on my local dev (PHP 5.4.x) but when I put it on my server PHP 5.3.x, it just doesn't work anymore :( Now I see 1 problem being that we can't use anonymous array like this []
but instead should be written the old way of array()
, that is inside the addConnection(array($settings))
great, now a little further...but then after it seems to crash inside $capsule->setEventDispatcher()
and I don't have logs on my server (I only found through var_dump() here and there), it's just a NAS and I don't want to even think of spending a few hours just to find out how to enable it. But what's funny is that I had a Laravel 4 project working with it... so why just building a part of it Illuminate\Database
doesn't work then?
I also found another piece of code to make Eloquent ORM work in PHP 5.3.x
$settings = array(
'driver' => '',
'host' => '127.0.0.1',
'database' => '',
'username' => '',
'password' => '',
'charset' => "utf8",
'collation' => 'utf8_general_ci',
'prefix' => ''
);
// Bootstrap Eloquent ORM
$connFactory = new \Illuminate\Database\Connectors\ConnectionFactory(new Illuminate\Container\Container);
$conn = $connFactory->make($settings);
$resolver = new \Illuminate\Database\ConnectionResolver();
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
\Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
but if I use this piece of code, which is good with Models by the way. I need to use $conn->table('...')...
instead of the Facade simple way of DB::table(....)
which is what I want, why is it important you would say? Well what if I want to convert to Laravel in the future... I would have to change all of the $conn->
to DB::
so I would rather do it right the first time. If someone knows how to create Facade on the 2nd piece of code, I would be happy too... Thanks for any help.
I'm also using Capsule with Slim. First off, make sure that you're using version 4.1.* of Illuminate/Capsule. When 4.2 came out, one of the big changes was that PHP 5.4 became the required minimum. 4.1 will still work with PHP 5.3.
Now, if you've done that and you're still running into problems, this is how I registered capsule with Slim's IoC container:
Note the call to
getConnection()
. This is necessary, as the Capsule global is not getting registered.Edit: Also note the call to
setFetchMode
. When using Fluent from within Laravel, queries return instances of stdClass, but the default behavior appears to be to return associative arrays. The framework just overrides that at some point, leading to Capsule behaving in correct, yet unexpected ways. This sets it to run with more expected behavior.