ziadoz/silex-capsule
最新稳定版本:2.0.0
Composer 安装命令:
composer require ziadoz/silex-capsule
包简介
A Laravel Eloquent ORM/Capsule service provider for Silex
README 文档
README
Note: This repository is no longer maintained, as the Silex has ended in favour of Symfony 4 Flex.
This is a service provider for the Silex Micro Framework that integrates Laravel's Eloquent ORM via Capsule, its standalone wrapper implementation.
Requirements
In order to use the service provider you'll need to be running PHP 5.5.9+
Installation
The best way to install the service provider is using Composer:
composer require ziadoz/silex-capsule:2.*
Alternatively, you can add it directly to your composer.json file:
{
"require": {
"ziadoz/silex-capsule": "2.*"
}
}
Basic Usage
To use it in your application just register the service provider with Silex:
<?php $app = new Silex\Application; $app->register(new Ziadoz\Silex\Provider\CapsuleServiceProvider, [ 'capsule.connection' => [ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'database', 'username' => 'username', 'password' => 'password', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'logging' => true, ], ]);
For more information about the available options you should refer to the Capsule documentation.
Once you've registered the service provider a Capsule object will be created and Eloquent booted up before any of your route controllers are called. If you're interested in the technical aspects, Capsule is registered as before middleware with Silex using Application::EARLY_EVENT, so it'll only ever be made available once your application is run and before anything else happens.
If you need Capsule and Eloquent to be booted up before your application is run ($app->run()), for example in a Symfony Console command, you just need to access its array element within the dependency injection container:
$app['capsule'];
Capsule will be made available globally by default, allowing you to write queries directly in your controllers should you wish:
use Illuminate\Database\Capsule\Manager as Capsule; $app->get('/book/{id}', function(Application $app, $id) { $book = Capsule::table('books')->where('id', $id)->get(); return $app->json($book); });
If you don't want Capsule to be booted then set the capsule.global setting to false. If you don't plan to use Eloquent to build models then you can also prevent it from being booted by setting capsule.eloquent to false.
Creating Eloquent models is identical to how you would create them in Laravel:
use Illuminate\Database\Eloquent\Model; class Book extends Model { protected $table = 'books'; protected $fillable = [ 'title', 'author', ]; protected $casts = [ 'title' => 'string', 'author' => 'string', ]; // The rest of your model code... }
Then you can use it, and all of its features, in your controllers just like you would in Laravel:
$app->get('/books', function(Application $app) { $books = Book::with('tags')->all(); return $app->json($books); }); $app->post('/book', function(Application $app, Request $request) { $book = new Book(); $book->title = $request->request->get('title'); $book->author = $request->request->get('author'); $book->save(); });
Advanced Usage
You can setup multiple connections and even caching with the service provider; simply use the capsule.connections option:
<?php $app = new Silex\Application; $app->register(new Ziadoz\Silex\Provider\CapsuleServiceProvider, [ // Connections 'capsule.connections' => [ 'default' => [ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'dname1', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'logging' => false, ], 'other' => [ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'dbname2', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'logging' => true, ], ], ]);
If you've enabled query logging on your connection you can retrieve it through Capsule:
Capsule::connection($name)->getQueryLog();
You can toggling query logging on a per-connection basis using the logging option in your connection credentials.
You can also use Eloquent's schema building tools to build migrations:
$app['capsule']->schema()->create('books', function($table) { $table->increments('id'); $table->string('title'); $table->string('author'); $table->timestamps(); });
By default the service provider installs the Laravel Events package, so you can also use model observers:
<?php class BookObserver { public function saving($model) { // Do something } } Book:observe(new BookObserver());
Capsule Options Example
The following is a full example of all the available options that you can pass to the service provider:
<?php $app = new Silex\Application; $app->register(new Ziadoz\Silex\Provider\CapsuleServiceProvider, [ // Multiple Connections 'capsule.connections' => [ 'default' => [ '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' => [ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'dbname2', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'logging' => true, // Toggle query logging on this connection. ], ], /* // Single Connection 'capsule.connection' => [ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'dbname', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'logging' => true, // Toggle query logging on this connection. ], */ /* // Other Options 'capsule.global' => true, // Enable global access to Capsule query builder. 'capsule.eloquent' => true, // Automatically boot Eloquent ORM. */ ]);
Testing
There are some basic tests to ensure that the Capsule object is correctly registered with Silex. You can run them using PHPUnit, and you'll also need SQLite as the tests use a simple in-memory database.
If you make a pull request please ensure you add the accompanying tests.
ziadoz/silex-capsule 适用场景与选型建议
ziadoz/silex-capsule 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.25k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2015 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「silex」 「database」 「orm」 「sql」 「service provider」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ziadoz/silex-capsule 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ziadoz/silex-capsule 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ziadoz/silex-capsule 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Kinikit - PHP Application development framework MVC component
Store your language lines in the database, yaml or other sources
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Object-oriented CRUD for Doctrine DBAL
统计信息
- 总下载量: 26.25k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 18
- 点击次数: 27
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-03-15