Skip to content

Instantly share code, notes, and snippets.

@totoprayogo1916
Last active September 2, 2022 02:28
Show Gist options
  • Save totoprayogo1916/abfe7b872d950bf5b6b639dc4947814d to your computer and use it in GitHub Desktop.
Save totoprayogo1916/abfe7b872d950bf5b6b639dc4947814d to your computer and use it in GitHub Desktop.
illuminate/eloquent setting for CodeIgniter4
<?php
// app/Config/Eloquent.php
namespace Config;
use Illuminate\Database\Capsule\Manager as Capsule;
class Eloquent
{
public function __construct()
{
$capsule = new Capsule();
$db_config = config('Database');
// // DBDriver
$capsule->addConnection([
'driver' => $db_config->default['DBDriver'],
'host' => $db_config->default['hostname'],
'database' => $db_config->default['database'],
'username' => $db_config->default['username'],
'password' => $db_config->default['password'],
'charset' => $db_config->default['charset'],
'collation' => $db_config->default['DBCollat'],
'prefix' => $db_config->default['DBPrefix'],
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
}
}
<?php
// app/Config/Event.php
namespace Config;
use CodeIgniter\Events\Events;
use CodeIgniter\Exceptions\FrameworkException;
/*
* --------------------------------------------------------------------
* Application Events
* --------------------------------------------------------------------
* Events allow you to tap into the execution of the program without
* modifying or extending core files. This file provides a central
* location to define your events, though they can always be added
* at run-time, also, if needed.
*
* You create code that can execute by subscribing to events with
* the 'on()' method. This accepts any form of callable, including
* Closures, that will be executed when the event is triggered.
*
* Example:
* Events::on('create', [$myInstance, 'myMethod']);
*/
Events::on('pre_system', static function () {
if (ENVIRONMENT !== 'testing') {
if (ini_get('zlib.output_compression')) {
throw FrameworkException::forEnabledZlibOutputCompression();
}
while (ob_get_level() > 0) {
ob_end_flush();
}
service('eloquent');
ob_start(static fn ($buffer) => $buffer);
}
/*
* --------------------------------------------------------------------
* Debug Toolbar Listeners.
* --------------------------------------------------------------------
* If you delete, they will no longer be collected.
*/
if (CI_DEBUG && ! is_cli()) {
Events::on('DBQuery', 'CodeIgniter\Debug\Toolbar\Collectors\Database::collect');
Services::toolbar()->respond();
}
});
<?php
// app/Config/Services.php
namespace Config;
use CodeIgniter\Config\BaseService;
/**
* Services Configuration file.
*
* Services are simply other classes/libraries that the system uses
* to do its job. This is used by CodeIgniter to allow the core of the
* framework to be swapped out easily without affecting the usage within
* the rest of your application.
*
* This file holds any application-specific services, or service overrides
* that you might need. An example has been included with the general
* method format you should use for your service methods. For more examples,
* see the core Services file at system/Config/Services.php.
*/
class Services extends BaseService
{
/*
* public static function example($getShared = true)
* {
* if ($getShared) {
* return static::getSharedInstance('example');
* }
*
* return new \CodeIgniter\Example();
* }
*/
public static function eloquent($getShared = true)
{
if ($getShared) {
return static::getSharedInstance('eloquent');
}
return new Eloquent();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment