Adding new application to Yii 2 Advanced Project Template

After writing the previous article I have realized that I solved the problem quite wrong. I added the example RESTful app controller to the frontend application in the multi-application Yii 2 Advanced Project Template environment.

I have also realized that the corresponding Yii 2 Guide is very, very poor and limited. It only tells you that you can have many applications (like backend, fronted and console) in the multi-application Yii 2 Advanced Project Template environment. But it leaves you completely guessing on how to actually add new application.

This quick article is my memo to remember steps that must be undertaken in order to achieve this.

Create new application

Since this is a continuity to the previous article I decided to focus on adding RESTful layer (application) to the multi-application Yii 2 Advanced Project Template environment. Therefore let’s call it rest application.

1. Register new application. Add the following line at the end of common\config\bootstrap.php:

Yii::setAlias('@rest', dirname(dirname(__DIR__)) . '/rest');

2. Create rest folder in main application folder and copy entire content (all subdirs) from console folder.

3. Create rest\web subfolder.

4. If there are any migrations in rest\migrations folder — delete them.

Configure rest application

We must do some changes into rest\config\main.php file:

1. If you are going to use User model (following previous article) then we must add it’s configuration to the components section of the configuration file:

'user' => [
    'identityClass' => 'common\models\User',

2. We must change controller’s namespace and we should change application’s identifier:

'id' => 'app-rest',
'controllerNamespace' => 'rest\controllers',

We can safely remove aliases i and controllers map:

'aliases' => [
    '@bower' => '@vendor/bower-asset',
    '@npm'   => '@vendor/npm-asset',
'controllerMap' => [
    'fixture' => [
        'class' => 'yii\console\controllers\FixtureController',
        'namespace' => 'common\fixtures',

If you are working on a brand new template then it might be a good idea to check database configuration — in common\config\main-local.php file:

'components' => [
    'db' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=localhost;dbname=yii',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',

Make sure that provided credentials and configuration are enough to access the database and that it has above denoted database created (execute migrations, if not).

Adding missing files

We have created rest application based on console application. But console applications in Yii 2 are quite specific and some elements are missing there (i.e. web-accessible web folder) and must be added manually.

1. Copy frontend\web\index.php file do rest\web folder.

2. Add UserController.php file to rest\controllers\ folder:

namespace rest\controllers;
use yii\rest\ActiveController;

class UserController extends ActiveController
    public $modelClass = 'common\models\User';

Light it up!

If everything went smudge and as planned then after executing following URL:


you should be able to see list of users, as shown in the example here.

Leave a Reply