What is YII2?
Install Yii2 Basic App
Run:
composer create-project --prefer-dist yiisoft/yii2-app-basic yii2-sharable
cd yii2-sharable
Configure MariaDB Connection
Edit config/db.php
:
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=yii_shared_db',
'username' => 'root', // Change as needed
'password' => '',
'charset' => 'utf8mb4',
];
2. Define a Sharable Model & Migration
Instead of relying on pre-existing database tables, use migrations for portability.
Create Migration for post
Table
Run:
php yii migrate/create create_post_table
Then, edit migrations/mXXXX_create_post_table.php
:
use yii\db\Migration;
class mXXXX_create_post_table extends Migration
{
public function safeUp()
{
$this->createTable('post', [
'id' => $this->primaryKey(),
'title' => $this->string()->notNull(),
'content' => $this->text()->notNull(),
'created_at' => $this->timestamp()->defaultExpression('CURRENT_TIMESTAMP'),
]);
}
public function safeDown()
{
$this->dropTable('post');
}
}
Run the migration:
php yii migrate
Generate the Model
php yii gii/model --tableName=post --modelClass=Post
3. Setup Controller and Views
Create Controller
controllers/PostController.php
namespace app\controllers;
use app\models\Post;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
class PostController extends Controller
{
public function actionIndex()
{
$posts = Post::find()->all();
return $this->render('index', compact('posts'));
}
public function actionView($id)
{
return $this->render('view', ['post' => $this->findModel($id)]);
}
protected function findModel($id)
{
if (($model = Post::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested post does not exist.');
}
}
Create Views
views/post/index.php
<?php foreach ($posts as $post): ?>
<h2><a href="<?= \yii\helpers\Url::to(['post/view', 'id' => $post->id]) ?>"><?= $post->title ?></a></h2>
<p><?= $post->content ?></p>
<?php endforeach; ?>
views/post/view.php
<h1><?= $post->title ?></h1>
<p><?= $post->content ?></p>
4. Share the Application
Package it for Sharing
- Exclude Unnecessary Files: Add a
.gitignore
file:bash/vendor/
/runtime/
/web/assets/
- Include
composer.lock
to lock dependencies. - Ensure Migrations Exist (
migrations/
folder).
Sharing Methods
- Git Repository: Push to a Git repo.
- Zip Archive: Compress & distribute.
- Dockerize: Create a
docker-compose.yml
for MariaDB.
5. Install on Another Machine
To deploy:
git clone <repo_url> yii2-sharable
cd yii2-sharable
composer install
php yii migrate
php yii serve
Visit: http://localhost:8080/index.php?r=post
Now you have a sharable Yii2 app with MariaDB, a basic model, and migrations for easy deployment!
You said:
ChatGPT said:
To create multiple Yii2 applications that share overhead files (framework, extensions, and vendor dependencies) while using MariaDB with a basic model, follow these steps:
1. Setup a Shared Yii2 Framework Directory
Instead of installing Yii2 for each app separately, create a shared directory:
Install Yii2 Framework Once
mkdir yii2-shared
cd yii2-shared
composer create-project --prefer-dist yiisoft/yii2-app-basic shared-framework
This shared-framework/
directory will store the Yii2 framework and vendor files.
2. Create Multiple Yii2 Apps Using Shared Framework
Create two Yii2 apps (app1
and app2
) without installing Yii2 dependencies separately.
mkdir -p apps/app1 apps/app2
cp -r shared-framework/{config,web,commands,views,models,controllers} apps/app1/
cp -r shared-framework/{config,web,commands,views,models,controllers} apps/app2/
Now, app1
and app2
contain only app-specific files, while shared-framework
holds the common files.
3. Modify Each Yii2 App to Use the Shared Framework
Update apps/app1/index.php
and apps/app2/index.php
:
<?php
// Define shared framework path
$sharedFrameworkPath = dirname(__DIR__, 2) . '/shared-framework';
require $sharedFrameworkPath . '/vendor/autoload.php';
require $sharedFrameworkPath . '/vendor/yiisoft/yii2/Yii.php';
$config = require $sharedFrameworkPath . '/config/web.php';
(new yii\web\Application($config))->run();
This ensures both apps use the same Yii2 framework and vendor files.
4. Configure MariaDB Database for Each App
Modify apps/app1/config/db.php
and apps/app2/config/db.php
:
For app1
:
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=app1_db',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
];
For app2
:
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=app2_db',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
];
5. Create a Shared Model (Optional)
Instead of separate models for each app, create a shared model:
Move models/Post.php
to shared-framework/models/Post.php
and update apps/app1/models/Post.php
and apps/app2/models/Post.php
:
<?php
namespace app\models;
require dirname(__DIR__, 2) . '/shared-framework/models/Post.php';
class Post extends \app\shared\models\Post {}
Now, both apps use the same Post
model from the shared framework.
6. Setup Shared Migrations
Use database migrations for both apps but store them in the shared framework:
mkdir shared-framework/migrations
php yii migrate/create --migrationPath=shared-framework/migrations create_post_table
Edit mXXXX_create_post_table.php
:
use yii\db\Migration;
class mXXXX_create_post_table extends Migration
{
public function safeUp()
{
$this->createTable('post', [
'id' => $this->primaryKey(),
'title' => $this->string()->notNull(),
'content' => $this->text()->notNull(),
'created_at' => $this->timestamp()->defaultExpression('CURRENT_TIMESTAMP'),
]);
}
public function safeDown()
{
$this->dropTable('post');
}
}
Run migration separately for each app:
php yii migrate --migrationPath=shared-framework/migrations --db=app1_db
php yii migrate --migrationPath=shared-framework/migrations --db=app2_db
7. Run Multiple Yii2 Apps
Start the first app:
cd apps/app1
php yii serve --port=8081
Start the second app:
cd ../app2
php yii serve --port=8082
Visit:
http://localhost:8081/index.php?r=post
http://localhost:8082/index.php?r=post
8. Summary
shared-framework/
contains Yii2 core files, vendor libraries, models, and migrations.apps/app1/
andapps/app2/
contain only configuration and application-specific files.- Single Yii2 installation is shared across multiple apps, reducing overhead.
Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.