bitolaco / silex-eloquent
Silex 应用程序的 Laravel Eloquent 服务提供程序
v0.1.1
2014-11-21 15:39 UTC
Requires
- php: >=5.4.0
- illuminate/database: ~4.2
- illuminate/events: ~4.2
- silex/silex: ~1.2
This package is not auto-updated.
Last update: 2024-09-28 16:17:55 UTC
README
此包为 Silex 应用程序提供 Laravel Eloquent 服务提供程序。它来自 此 gist,由 Jamie York 创建。这是他的作品,不是我们的。我们只是维护这个版本,用于我们的公共和客户项目。
以下说明展示了基本用法,并假设您已经熟悉 Eloquent 以及创建模型等。
Silex 2 和未来
有将此更新到 Silex 2.x 和 Illuminate 5.1 的基本代码,但由于 Lumen 已经出现,似乎在未来维护此项目的 Silex 2 将是多余的。如果您需要 Eloquent 和一个微框架用于新项目,Lumen 似乎将是更好的选择。
话虽如此,如果有人有兴趣帮助测试已在 silex-v2 分支 中的代码,请告诉我们!
除非发生这种情况,我们计划仅维护到 Silex 1.X 的生命周期结束。
安装
此包可以通过 Composer 安装。只需将其添加到您的 composer.json
文件中作为依赖项。
{ "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. ) ) ) );
APC 缓存示例
$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' ), ) );
启动和使用
只有在调用 $app->run() 时,Silex 才会建立数据库连接,这是当您需要手动在之前建立连接时需要调用 $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));