Yii2 – Setup Advance Application

  • Avinash
  • 4
  • Aug 27, 2015
  • Tutorial

In the previous article for the Yii2 Installation, I have mentioned dealing with Advance template of Yii2 for further articles. Based on that, this article is specifically I am writing to setup our advance Yii2 application such a way that is easy to access.

Yii2 Setup Advance Application

Earlier we have seen URLs for accessing frontend and backend, those are like very bad and no client would be happy after seeing those URLs for their website. Just to recap we have seen below URLs.

Front End: http://192.168.0.2:26/frontend/web/
Back End: http://192.168.0.2:26/backend/web/

But normally what we want is URLs like below:

Front End: http://192.168.0.2:26/
Back End: http://192.168.0.2:26/admin

We are going to cover this thing in the article, I will follow few minor edits in configuration files and we are done with this.

Edit Configuration

/frontend/config/main.php

Open you config file of frontend layer and make following changes in that. If you are already having array element then you can modify as mentioned below else create a new element.

return [ 'homeUrl' => '/', 'components' => [ 'request' => [ 'baseUrl' => '', ], 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, ], ], ];
return  [
            'homeUrl' => '/',
            'components' => [
                'request' => [
                    'baseUrl' => '',
                ],
                'urlManager' => [
                    'enablePrettyUrl' => true,
                    'showScriptName' => false,
                ],
            ],
        ];

/backend/config/main.php

Here are the edits for backend layer configuration file.

return [ 'homeUrl' => '/admin', 'components' => [ 'request' => [ 'baseUrl' => '/admin', ], 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, ], ], ];
return  [
            'homeUrl' => '/admin',
            'components' => [
                'request' => [
                    'baseUrl' => '/admin',
                ],
                'urlManager' => [
                    'enablePrettyUrl' => true,
                    'showScriptName' => false,
                ],
            ],
        ];

Please note that I have added only required things in above edits. So you may have other things in the configuration file which will remain unchanged.

Modify .htaccess file

You need to add below mentioned code in .htaccess file which is there in the root of your application. If you do not have one then you need to create .htaccess file in the root of you advance application.

Options FollowSymLinks AddDefaultCharset utf-8 RewriteEngine On # the main rewrite rule for the frontend application RewriteCond %{REQUEST_URI} !^/(backend/web|admin) RewriteRule !^frontend/web /frontend/web%{REQUEST_URI} [L] # redirect to the page without a trailing slash #RewriteCond %{REQUEST_URI} ^/admin/$ #RewriteRule ^(admin)/ /$1 [L,R=301] # the main rewrite rule for the backend application RewriteCond %{REQUEST_URI} ^/admin RewriteRule ^admin(.*) /backend/web/$1 [L] # if a directory or a file of the frontend application exists, # use the request directly RewriteCond %{REQUEST_URI} ^/frontend/web RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # otherwise forward the request to index.php RewriteRule . /frontend/web/index.php [L] # if a directory or a file of the backend application exists, # use the request directly RewriteCond %{REQUEST_URI} ^/backend/web RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # otherwise forward the request to index.php RewriteRule . /backend/web/index.php [L] RewriteCond %{REQUEST_URI} \.(htaccess|htpasswd|svn|git) RewriteRule \.(htaccess|htpasswd|svn|git) - [F]
Options FollowSymLinks
AddDefaultCharset utf-8
                           
<ifmodule mod_rewrite.c>
   RewriteEngine On

   # the main rewrite rule for the frontend application
  RewriteCond %{REQUEST_URI} !^/(backend/web|admin)
   RewriteRule !^frontend/web /frontend/web%{REQUEST_URI} [L]

   # redirect to the page without a trailing slash
  #RewriteCond %{REQUEST_URI} ^/admin/$
  #RewriteRule ^(admin)/ /$1 [L,R=301]
  # the main rewrite rule for the backend application
  RewriteCond %{REQUEST_URI} ^/admin
   RewriteRule ^admin(.*) /backend/web/$1 [L]

   # if a directory or a file of the frontend application exists,
  # use the request directly
  RewriteCond %{REQUEST_URI} ^/frontend/web
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   # otherwise forward the request to index.php
  RewriteRule . /frontend/web/index.php [L]

   # if a directory or a file of the backend application exists,
  # use the request directly
  RewriteCond %{REQUEST_URI} ^/backend/web
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   # otherwise forward the request to index.php
  RewriteRule . /backend/web/index.php [L]

   RewriteCond %{REQUEST_URI} \.(htaccess|htpasswd|svn|git)
   RewriteRule \.(htaccess|htpasswd|svn|git) - [F]
</ifmodule>

Once you have successfully completed above steps than we are ready to access our frontend and backend at above mentioned URLs.

Front End: http://192.168.0.2:26/

Yii2 FrontEnd Setup

Back End: http://192.168.0.2:26/admin

Yii2 Backend Setup

Up Next

Hope you have setup your application successfully as explained above. In next article, we will see, how to create a separate session for Frontend and backend tiers. Like our facebook page to get updates on latest articles in this series.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by:

  • sankar

    Okay fine. But having the doubt here need to add two virtual hosts for both front & back end.

    • http://www.xpertdeveloper.com/ Avinash

      If that is the case then I think you can directly map the layer directory. i.e. backend and frontrend. I have not tried this just guessing.

  • http://www.xpertdeveloper.com/ Avinash

    Here I want clean urls in backend also.

    for this i think you forgot to make configuration changes for config file of backend tier.

  • http://www.xpertdeveloper.com/ Avinash

    If you are looking for different domain for both frontend and backend the best to look at https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/start-installation.md

    It is a same think you are looking for.