Set up a Yii 2.0 Application in Windows

ows_ali

Owais Ali

Posted on November 12, 2018

Set up a Yii 2.0 Application in Windows

PHP Yii 2.0 Framework

Let’s setup a Yii 2.0 Application on local PC. We will setup Yii 2.0 advanced application on Windows alongwith:

  • Connection with MySQl
  • Enabling Pretty URLs
  • Virtual Hosts (for backend and frontend)
  • Installing AdminLTE Theme

Installing the project

Let’s install the application in our PC using Composer (you should install composer first if you don’t have already).

Head over to your xampp/htdocs folder and run this command in cmd:

composer create-project --prefer-dist yiisoft/yii2-app-advanced ows-yii2-application

The application will be installed in ‘ows-yii2-application’ directory/folder. You can rename it according to your project name

Enter into the folder ‘ows-yii2-application’ in cmd and then run the command:

init

Choose ‘0’ or ‘dev’ to initialize the application in development environment.

Run the following command to install dependencies:

composer install 

You now have the working application in your local PC. Turn on your Apache server using XAMPP/WAMP or any other server that you use and headover to

localhost/ows-yii2-application/backend/web

and

localhost/ows-yii2-application/frontend/web

DB Connection with MySQL

Make a database in your local machine with name ‘ows_database’. To connect to this database, go to common/config/main-local.php and set dbname to

'dsn' => 'mysql:host=localhost;dbname=ows\_database',
'username' => 'root',
'password' => ''

(and edit the username and password also if you have changed them otherwise leave it as it is)

Now, run the command

yii migrate

This will generate the user table in your database and RBAC tables as well for future use.

Now, Go to

http://localhost/ows-yii2-application/frontend/web/index.php?r=site%2Fsignup

And create a user with

username : testusername

password : testpassword

Now, you can login into the application using these credentials.

Enabling Pretty URLs

Currently, the URL looks like:

http://localhost/ows-yii2-application/backend/web/index.php?r=site%2Flogin

but we want it to be as follows:

http://localhost/ows-yii2-application/backend/web/login

In order to enable pretty URLs, go to backend\config\main.php and uncomment the following code in $components‘urlManager’:

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '<alias:\w+>' => 'site/<alias>',
            ],
        ],

Make a .htaccess file with the following code and place it in backend/web folder:

RewriteEngine on

RewriteCond %{REQUEST\_FILENAME} !-f
RewriteCond %{REQUEST\_FILENAME} !-d
RewriteRule . index.php

Do the same in frontend application.

Virtual Hosts

Now, we want to run our backend and frontend applications from two different and easy to remember domains/server names. Like:

admin.ows-yii2-application.com for backend application

and

ows-yii2-application.com for frontend application.

In order to achieve that, copy and paste the following in your httpd-vhosts file, most likely on ‘xampp\apache\conf\extra\httpd-vhosts’ (Make sure to change the DocumentRoot to set it as your own web directory)

<VirtualHost \*:80>
       ServerName admin.ows-yii2-application.com
       DocumentRoot "E:\xampp\htdocs\ows-yii2-application\backend\web"
</VirtualHost>

<VirtualHost \*:80>
       ServerName ows-application.com
       DocumentRoot "E:\xampp\htdocs\ows-yii2-application\frontend\web"
</VirtualHost> 

and the following in your hosts file at C:\Windows\System32\drivers\etc (you may be required to open the file in Administrator mode):

127.0.0.1 admin.ows-yii2-application.com
127.0.0.1 ows-yii2-application.com

Restart your Apache server.

Now, you can access your backend and frontend applications on admin.ows-yii2-application.com and ows-yii2-application.com respectively.

Installing AdminLTE Theme in Yii 2.0 Application

Let’s now setup the AdminLTE Theme with our application as well. Run the following command

composer require dmstr/yii2-adminlte-asset "^2.1"

Now, go to frontend/config/main.php and add this in components[‘view’]

'view' => [
            'theme' => [
                'pathMap' => [
                    '@app/views' => '@vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app'
                 ],
             ],
        ],

Follow the similar steps for the backend application if you want to enable the AdminLTE theme in backend application too.

Conclusion

So we have successfully set up our Yii 2.0 Application, a great PHP Framework. You can find all this stuff in my repository at:

https://github.com/ows-ali/ows-yii2-application

if you don’t understand anything. Also, if you are new to Github and want to learn the basics, then give it a read:

Learn Git and Github Step By Step

Thank you for reading. If you liked the article, please give it a clap.

💖 💪 🙅 🚩
ows_ali
Owais Ali

Posted on November 12, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Set up a Yii 2.0 Application in Windows
framework Set up a Yii 2.0 Application in Windows

November 12, 2018