Set up a Yii 2.0 Application in Windows
Tags: framework, phpdevelopers, php, yii2
Published: 2018-11-12 12:30:26

---
title: Set up a Yii 2.0 Application in Windows
published: true
tags: framework,php-developers,php,yii2
canonical_url: https://medium.com/@ows_ali/set-up-a-yii-2-0-application-in-windows-bb54ce763e51
---

![](https://cdn-images-1.medium.com/max/650/0*0Ci9Hikv6--WF2Dq.jpg)<figcaption>PHP Yii 2.0 Framework</figcaption>

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](http://localhost/practice/ows-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](http://localhost/ows-application/backend/web/index.php?r=site%2Flogin)

but we want it to be as follows:   
[http://localhost/ows-yii2-application/backend/web/login](http://localhost/ows-application/backend/web/login)

In order to enable pretty URLs, go to backend\config\main.php and uncomment the following code in $components[‘urlManager’] (do not forget to add the rules as well):

```
'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](https://dev.to/ows_ali/learn-git-and-github-step-by-step-536i-temp-slug-5321610)

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