Web Development

Yii2 – Setup Advance Application

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 set up our advanced Yii2 application in 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.

[gads]

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.

[cc lang=”php”]
return [
‘homeUrl’ => ‘/’,
‘components’ => [
‘request’ => [
‘baseUrl’ => ”,
],
‘urlManager’ => [
‘enablePrettyUrl’ => true,
‘showScriptName’ => false,
],
],
];
[/cc]

/backend/config/main.php

Here are the edits for backend layer configuration file.

[cc lang=”php”]
return [
‘homeUrl’ => ‘/admin’,
‘components’ => [
‘request’ => [
‘baseUrl’ => ‘/admin’,
],
‘urlManager’ => [
‘enablePrettyUrl’ => true,
‘showScriptName’ => false,
],
],
];
[/cc]

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.

[gads]

Modify .htaccess file

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

[cc lang=”php”]
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]

[/cc]

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

[gads]

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.

Shares:

Leave a Reply

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