定制 jguyomard/silex-capsule-eloquent 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jguyomard/silex-capsule-eloquent

最新稳定版本:v2.0.2

Composer 安装命令:

composer require jguyomard/silex-capsule-eloquent

包简介

Capsule/Eloquent Service Provider for Silex 2

README 文档

README

Travis StyleCI Packagist Release Licence

This is a Service Provider for Silex 2.0 that integrates Laravel's Fluent Query Builder and Eloquent ORM via Capsule.

Installation

Note: This Service Provider requires silex/silex ~2.0.

composer require jguyomard/silex-capsule-eloquent "~2.0"

Usage

This is a basic configuration with MySQL (Currently, Laravel supports MySQL, Postgres, SQLite and SQL Server):

$app = new Silex\Application();

$app->register(
    new \JG\Silex\Provider\CapsuleServiceProvider(),
    [
        'capsule.connections' => [
            'default' => [
                'driver'    => 'mysql',
                'host'      => 'localhost',
                'database'  => 'mydatabase',
                'username'  => 'root',
                'password'  => 'root',
            ]
        ]
    ]
);

This is a basic usage, using Query Builder or Raw SQL Queries:

$app->get('/article/{id}', function(Application $app, $id)
{
    $article = Capsule::table('article')->where('id', $id)->get();

    // Rest of your code...
});

$app->get('/raw/{id}', function(Application $app, $id)
{
    $article = Capsule::select('SELECT * FROM article WHERE id = :id', [
        'id' => $id,
    ]);

    // Rest of your code...
});

$app->run();

You can also use Eloquent Models:

class ArticleModel extends Model
{
    protected $table = 'article';

    protected $primaryKey = 'id';

    protected $fillable = [
        'title'
    ];

    // Rest of your code...
}

$app->get('/article/{id}', function(Application $app, $id)
{
    $article = ArticleModel::find($id);

    // Rest of your code...
});

$app->post('/article', function(Application $app)
{
    $article = ArticleModel::create([
        'title' => 'Foo'
    ]);

    // Rest of your code...
});

$app->run();

For further documentation on using the various database facilities this library provides, consult the Laravel framework database documentation.

Configuration

This is a complete configuration example, with multiple connections:

$app = new Silex\Application();

$app->register(
    new \JG\Silex\Provider\CapsuleServiceProvider(),
    [
        'capsule.connections' => [
            'default' => [
                'driver'    => 'mysql',
                'host'      => 'localhost',
                'port'      => 3306,
                'database'  => 'mydatabase',
                'username'  => 'root',
                'password'  => 'root',
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
                'strict'    => false,
                'engine'    => null,
            ],
            'pgsql' => [
                'driver' => 'pgsql',
                'host'      => 'localhost',
                'port'      => 5432,
                'database'  => 'mydatabase',
                'username'  => 'root',
                'password'  => 'root',
                'charset'   => 'utf8',
                'prefix'    => '',
                'schema'    => 'public',
            ],
            'sqlite' => [
                'driver' => 'sqlite',
                'database'  => 'mydatabase',
                'prefix' => '',
            ],
        ],
        'capsule.options' => [
            'setAsGlobal'    => true,
            'bootEloquent'   => true,
            'enableQueryLog' => true,
        ],
    ]
);

Testing

To run the test suite, you need PHPUnit:

phpunit

Credits

Inspired by illuminate-database-silex-service-provider (for Silex 1.*) and saxulum-doctrine-mongodb-odm-provider@dev (Mongodb ODM for Silex 2.0.x-dev).

Issues

If you have any problems with or questions about this Service Provider, please contact me through a GitHub issue. If the issue is related to Capsule itself please leave an issue on Laravel official repository.

Contributing

You are invited to contribute new features, fixes or updates to this container, through a Github Pull Request.

统计信息

  • 总下载量: 19.72k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 2
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-04-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固