Created
March 24, 2020 13:09
-
-
Save rbarros/03af76dd4a4a734bfe61f3b2a43f8a91 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Capsule\Manager as Capsule; | |
use Illuminate\Events\Dispatcher; | |
use Illuminate\Container\Container; | |
/** | |
* Slim 3.x shipped with the Pimple container implementation and enabled the following synthax | |
*/ | |
$app = new \Slim\App([...]); | |
$container = $app->getContainer(); | |
$capsule = new Capsule; | |
$capsule->addConnection( | |
[ | |
'driver' => 'mysql', | |
'host' => env('DB_HOST', 'localhost'), | |
'database' => env('DB_DATABASE', 'portal'), | |
'username' => env('DB_USERNAME', 'root'), | |
'password' => env('DB_PASSWORD', ''), | |
'charset' => 'utf8', | |
'collation' => 'utf8_unicode_ci', | |
'prefix' => '' | |
] | |
); | |
// Set the event dispatcher used by Eloquent models... (optional) | |
$capsule->setEventDispatcher(new Dispatcher(new Container)); | |
// Make this Capsule instance available globally via static methods... (optional) | |
$capsule->setAsGlobal(); | |
$capsule->connection()->enableQueryLog(); // enable log | |
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher()) | |
$capsule->bootEloquent(); | |
// Configura o retorno da consulta | |
// PDO::FETCH_OBJ == object | |
// PDO::FETCH_ASSOC = array | |
$capsule->setFetchMode(PDO::FETCH_ASSOC); | |
/** | |
* Adiciona a instancia do banco ao Slim | |
*/ | |
$container['db'] = function ($c) use ($capsule) { | |
return $capsule; | |
}; | |
/** | |
* Slim 4.x does not ship with a container library. It supports all PSR-11 implementations such as PHP-DI | |
* To install PHP-DI `composer require php-di/php-di` | |
*/ | |
use Slim\Factory\AppFactory; | |
$container = new \DI\Container(); | |
AppFactory::setContainer($container); | |
$app = AppFactory::create(); | |
$container = $app->getContainer(); | |
$container->set('db', function(\Psr\Container\ContainerInterface $container){ | |
return $capsule; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment