Hope you are ready to give a kick off to this article series and we are about to start the installation process of Yii2. In the previous article, we have seen about what’s new in Yii2 and some of the good feature which makes me choose Yii2 over another framework.
We have two different ways to install Yii2 while with an earlier version of Yii we had only one way to install it. We will go through both the ways to install Yii.
The composer is getting more and more attention nowadays because it makes your life easier when you are dealing with multiple dependencies. The composer does all required stuff to manage your dependencies, you just have to manage those in one single JSON file which is composer.json.
If you are new with composer and/or never dealt with it, then I would suggest go through the documentation of Composer to install and get started. At this stage, I am assuming that you have composer installed and you are comfortable to use it.
Once ready, open your terminal and reach to your web accessible directory. In most cases for Linux flavor its /var/www and for windows you htdocs directory under apache. This path may vary based on the environment you have setup.
After this command executes, it will download Yii2 along with its all dependencies.
composer global require "fxp/composer-asset-plugin:~1.0.0"
composer create-project --prefer-dist yiisoft/yii2-app-basic basic
Similar to Composer we have another package managers available in the market which are bower and NPM. It will allow you to manage Bower and/or NPM dependencies with Composer. This is a one-time command you don’t need to run it for all project you setup.
The second command actually downloads Yii2 and other dependencies inside the directory named basic, which we have mentioned at last in our command. We can use any other name instead of basic. Here you can see that we are installing Yii using composer so we can say that Yii2 itself a one of the dependency.
This command will download the basic version of Yii2 framework. Here don’t go by the name, it really doesn’t mean that it has limited features, but actually it refers to default setup they are providing. With Yii2, we can have two different types of applications which are basic and advance. Both types are having the same framework behind, but an advance version is giving us several layers to deal with like frontend, backend, and console while in the basic version we do not have such layers. It’s all in one layer and we have to managed it accordingly.
As composer will download some of the dependencies from Github, it may ask for Access token to download the same. Don’t worry, just go to your Github account and create a one for this.
After downloading is completed, you can see that it has downloaded all the dependencies under vendor directory, you can refer the same in an image below.
The second command was to download Yii2 basic application but if you want to download advance version of Yii2 then you need to run command instead of a previous one.
composer create-project --prefer-dist yiisoft/yii2-app-advanced yii-advance
After advanced application download is done, you can see that under vendor directory it has download loaded same dependencies. So as a base it is using the same things but a structure of an application is bit different.
With the advance application, you can see that we have three different layers for application which are frontend, backend, and console.
This application has got one more directory called common which is shared among all layers. So if you want something to be available to all layer you can use this directory.
If you don’t want to go with composer way (which is recommended), then you can manually download Yii2 framework from an official website.
You can extract this downloaded files into a web-accessible directory to access.
Up to this stage, we have successfully installed the basic and advanced version of Yii2 framework. Moving forward we will be dealing with an advanced version of framework because it gives us a better structure of an application in different layers.
Once you have a downloaded framework, we need to follow steps to initialize our application. These steps are required for all projects you setup up.
At the root of your application, you can see that we have the file which we can run from command line, which is init.
Once you execute this file, it will ask you series of questions and setup can go on. As we are setting up this application for study purpose, I will go with selecting Development as a mode.
Once a process is completed, it will create required files to start a project. You can see the same in an image above, it shows the list of files which are created.
At this stage, we have our application is initialized and ready to use. You can access your application based on the path you have installed. Normally I used to work by creating Virtual Host for each and every project I deal with. You can refer to my article on How to create Virtual Host for more information.
If everything does well then you can access your project at below URLs. Please note URL, it’s because of virtual host I have set. This can be different based on a setup you have.
Frontend Tier: http://192.168.0.2:26/frontend/web/
Backend Tier: http://192.168.0.2:26/backend/web/
Here you can see that backend is password protected because advance application ships with basic functionalities implemented like Login, Logout, and Access Control.
At this stage, I hope you are comfortable installing Yii2 without any issues. In Next article, we will deal with Advance Application of Yii2. We will make some changes in that so that we can access an application in an easy way.
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: Avinash