Adding a new app in Yii2 Advanced app

Yii2 advanced app already contains three main folders (frontend,backend,common) that can help you to start your project.

As their names suggests,frontend can be used to create your apps front interface,the backend can be the administration end to manage the whole app.

The commons folder contains files or assets that are required by both the frontend and the backend.

Sometimes the three folders are not enough and you want to add an additional app (when building the next billion dollar business).How do you go about this?

First things first! Make sure you have installed Yii advanced template on your serve(I use XAMPP on my local computer).

For illustration purposes I have installed Yii 2 advanced template on the localhost.

Step 1: Make a new folder in the root directory name it anything you want (named shop in my case).

After this step you must be having an extra folder i.e frontend,common,backend and shop.

Step 2 :Copy the contents of frontend into the newly created shop folder.

Step 3 : Edit the config files

Navigate to shop/config folder and edit the config files.

You will have to change the app ID,controllerNamespace,app components like request and session in the main.php.

See example :

'id' => 'shop',
'controllerNamespace' => 'shop\controllers',

'components' => [
'request' => [
'csrfParam' => '_csrf-shopping-cart',

'cookieValidationKey' => 'fjdsjjkdkkassdsdd'
],
'session' => [
// this is the name of the session cookie used for login on the shopping-cart
'name' => 'frontend-shopping-cart',
]

]

 

Modify the controller and model namespace in the shop/controllers  and shop/models files (where applicable) .

 

Step 4: Add app alias to common/config/bootstrap.php

Yii::setAlias('@common', dirname(__DIR__));
Yii::setAlias('@frontend', dirname(dirname(__DIR__)) . '/frontend');
Yii::setAlias('@backend', dirname(dirname(__DIR__)) . '/backend');
Yii::setAlias('@console', dirname(dirname(__DIR__)) . '/console');
Yii::setAlias('@api', dirname(dirname(__DIR__)) . '/api');
Yii::setAlias('@shop', dirname(dirname(__DIR__)) . '/shop')

 

 

Step 5: Add your app to the .htaccess file .

This step will be optional depending on how you configured the .htaccess file.

# deal with shop
RewriteCond %{REQUEST_URI} ^/projects/YiiRestful/(shop)
RewriteRule ^shop/assets/(.*)$ shop/web/assets/$1 [L]
RewriteRule ^shop/css/(.*)$ shop/web/css/$1 [L]
RewriteRule ^shop/js/(.*)$ shop/web/js/$1 [L]
RewriteRule ^shop/images/(.*)$ shop/web/images/$1 [L]

 

Now you should be able to access your new app on http://localhost/projects/YiiRestful/shop/

Thats it guys, let me know your ideas  guys so that I can improve where possible 🙂

 

Relate posts

Yii RESTful guide

 

Leave a Reply

Your email address will not be published. Required fields are marked *