1. Setup a Shared Yii2 Framework Directory
Instead of installing Yii2 for each app separately, create a shared directory:
Install Yii2 Framework Once
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.
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
:
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
:
For app2
:
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
:
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:
Edit mXXXX_create_post_table.php
:
Run migration separately for each app:
7. Run Multiple Yii2 Apps
Start the first app:
Start the second app:
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.