USA

1. Setup a Shared Yii2 Framework Directory

Instead of installing Yii2 for each app separately, create a shared directory:

Install Yii2 Framework Once

bash
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.

bash
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
<?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:

php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=app1_db',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
];

For app2:

php
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
<?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:

bash
mkdir shared-framework/migrations
php yii migrate/create --migrationPath=shared-framework/migrations create_post_table

Edit mXXXX_create_post_table.php:

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:

bash
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:

bash
cd apps/app1
php yii serve --port=8081

Start the second app:

bash
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/ and apps/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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Page dynamically generated on

Brooks Computing Systems, LLC
Quality, Reliability & Service