This package provides a Laravel Eloquent service provider for Silex applications. It was pulled from this gist created by Jamie York. It is his creation, not ours. We just maintain this version for use in our public and client projects.
The instructions below show basic usage, and assume that you're already familiar with Eloquent and creating models, etc.
There's the basic code for updating this to Silex 2.x and Illuminate 5.1, but since Lumen has arrived on the scene, it seems that it would be a bit redundant to work on maintaining the project in the future for Silex 2. If you need Eloquent and a microframework for new proejcts, Lumen would seem to be the way to go.
That being said, if someone is interested in helping test the code already there in the silex-v2 branch, let us know!
Unless that happens, we plan to maintain this until the end of life for Silex 1.X only.
This package is available to install via Composer. Just add
it to your composer.json
file as a requirement:
{
"require": {
"bitolaco/silex-eloquent": "*"
}
}
$app = new Silex\Application;
$app->register(
new \BitolaCo\Silex\CapsuleServiceProvider(),
array(
'capsule.connection' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'logging' => true, // Toggle query logging on this connection.
)
)
);
<?php
$app = new Silex\Application;
$app->register(
new \BitolaCo\Silex\CapsuleServiceProvider(),
array(
// DB Connection: Multiple.
'capsule.connections' => array(
'default' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dname1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'logging' => false, // Toggle query logging on this connection.
),
'other' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dbname2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'logging' => true, // Toggle query logging on this connection.
)
)
)
);
$app = new Silex\Application;
$app->register(
new \BitolaCo\Silex\CapsuleServiceProvider(),
array(
'capsule.connection' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'logging' => true, // Toggle query logging on this connection.
),
'capsule.cache' => array(
'driver' => 'apc',
'prefix' => 'laravel',
),
)
);
$app = new Silex\Application;
$app->register(
new \BitolaCo\Silex\CapsuleServiceProvider(),
array(
'capsule.connection' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'logging' => true, // Toggle query logging on this connection.
),
'capsule.cache' => array(
'driver' => 'file',
'path' => '/path/to/cache',
'connection' => null,
'table' => 'cache',
'prefix' => 'laravel'
),
)
);
A connection to the database is only established once Silex is booted, which happens when you call $app->run(). If you need to establish the connection manually before then, you need to call $app['capsule'];
<?php
require __DIR__ . '/vendor/autoload.php';
$app = new Silex\Application();
$app->register(new \BitolaCo\Silex\CapsuleServiceProvider(), array(
'capsule.connection' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'test',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
));
$app['capsule'];
class Book extends Illuminate\Database\Eloquent\Model
{
protected $table = "books";
}
var_dump(Book::find(1));