Initialising The Yii 2 Advanced Template

Yii2 Hello World!

In our previous articles, we downloaded and went through the directory structure of the Yii 2 advanced template. Still, we haven't seen anything on the browser except the directory structure at https://localhost/advanced. Initialisation of the template is the process through which Yii generate few files (more specifically, copies from advanced/environment directory to app-specific directory) which are necessary for running the web app, frontend and backend. The files that are generated are index.php, index-test.php, params-local.php, main-local.php, etc.

The Yii 2 advanced template comes with two inbuilt environments - Development and Production. The files specific to these two environments are stored in the environments directory. More on the environment configurations and setup in the later articles.

Yii2 Advanced Directory Without Initialisation - BrowserAs you can see from the above snap, when we open the http://localhost/advanced/frontend/web/ in the browser we get to see the directory structure. This is because there is no index.php or index.html file by default in the downloaded template. Take a look at the directory structure below. There is no index.php file present in the backend/web or frontend/web.


Yii 2 Directory Without Initialisation - Code


The reason there is no index.php files is because Yii want's you to generate these files based on your environment. If you are on production environment then you can initialize the app as Production and if on development, then as Development environment. Let's open the terminal and navigate to the advanced directory using the cd command. Enter ./init and hit enter.

Yii 2 Initialisation CommandAs soon as you hit enter you see some commands running through your console. These commands copy the files form advanced/environments directory to backend and frontend directories. You can thus set up your configurations in these environment files and push it to your repository. While deploying the code you can just initialise the Yii 2 app in the specific environment.

Yii 2 Directory After Init - Browser

Once the process is completed, navigate to http://localhost/advanced/frontend/web/, you do not see the directory structure, but the above screen. Thus our app is initialised successfully and we are ready to start with our coding.


Yii 2 After Init - Code


Also, if you check the files in the advanced directory, you have some new files in the backend/web, backend/config, frontend/web, frontend/config, common/config, console/config directories. The DocumentRoot of our website will be fronted/web and for the admin panel, it will be backend/web.


DocumentRoot is the directory which contains most of the HTML/PHP files which are served in response to requests. In simple terms, it is the directory which accepts all the incoming requests and sends the response to the client. For simple PHP web apps, it must contain at least index.html or index.php files.


Now that our app is initialised we will start writing some code to get acquainted with the Yii 2 Framework. Before signing off, I recommend you to go through the output on the command line to get a glimpse of the commands executed and the generated files.