Skip to content

Instantly share code, notes, and snippets.

@mdmunir
Created May 27, 2021 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdmunir/e2a04d4a4d09b78c4f910a97c802087a to your computer and use it in GitHub Desktop.
Save mdmunir/e2a04d4a4d09b78c4f910a97c802087a to your computer and use it in GitHub Desktop.
Menggunakan Yii di Laravel
<?php
// file bootstrap/app.php
require_once __DIR__ . '/yii.php';
/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/
$app = new Illuminate\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
/*
|--------------------------------------------------------------------------
| Bind Important Interfaces
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
*/
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
/*
|--------------------------------------------------------------------------
| Return The Application
|--------------------------------------------------------------------------
|
| This script returns the application instance. The instance is given to
| the calling script so we can separate the building of the instances
| from the actual running of the application and sending responses.
|
*/
return $app;
<?php
// file bootstrap/yii.php
defined('YII_ENABLE_ERROR_HANDLER') or define('YII_ENABLE_ERROR_HANDLER', false);
use App\Models\User as UserModel;
use Illuminate\Support\Facades\DB;
use yii\console\Application as ConsoleApplication;
use yii\db\Connection;
use yii\web\Application as WebApplication;
use yii\web\User as WebUser;
require dirname(__DIR__) . '/vendor/yiisoft/yii2/Yii.php';
$config = [
'id' => 'app',
'basePath' => dirname(__DIR__),
'aliases' => [
'@App' => dirname(__DIR__) . '/App',
],
'components' => [
'db' => function () {
$pdo = DB::connection()->getPdo();
$driverName = DB::connection()->getDriverName();
return new Connection([
'pdo' => $pdo,
'driverName' => $driverName
]);
},
]
];
if (PHP_SAPI === 'cli') {
new ConsoleApplication($config);
} else {
$config['components']['user'] = function () {
$user = new WebUser([
'enableSession' => false,
'identityClass' => UserModel::class,
]);
if (($model = auth()->user())) {
$user->switchIdentity($model);
}
return $user;
};
$config['components']['request'] = [
'enableCsrfValidation' => false,
'enableCookieValidation' => false,
'enableCsrfCookie' => false,
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
];
new WebApplication($config);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment