Giter Club home page Giter Club logo

silex-capsule-eloquent's Introduction

Capsule (and Eloquent) Service Provider for Silex 2

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.

silex-capsule-eloquent's People

Contributors

jguyomard avatar matracine avatar rakitin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

silex-capsule-eloquent's Issues

Use with Symfony console

Hello,

I'm trying to use CapsuleServiceProvider in a Symfony 3.1 console project.
I managed to have it working by replacing all "Silex\Application $app" in closures' paramters with "Pimple\Container $app" in the register method.

I have to boot the capsule provider manualy by calling $app['capsule'].

It works perfectly for me. Is it possible to merge this in your repo ?
Going to make a pull request.

Version tag?

Could you, please, add version tag (actually anything, even 0.0.0 works) and then make sure it is updated in packagist also? This helps in speeding up composer installs as it starts caching package and no longer connect github on every install or update.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.