bitolaco/silex-eloquent

Laravel Eloquent service provider for Silex applications

v0.1.1 2014-11-21 15:39 UTC

This package is not auto-updated.

Last update: 2024-11-09 16:53:22 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

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.

Silex 2 and the future

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.

Installation

This package is available to install via Composer. Just add it to your composer.json file as a requirement:

{
	"require": {
		"bitolaco/silex-eloquent": "*"
	}
}

Examples

Single 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.
		)
	)
);

Multiple Connections

<?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.
			)
		)
	)
);

APC Caching Example

$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',
		),
	)
);

File Caching Example

$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'
		),
	)
);

Booting and Usage

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));