Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Getting started with Yii 2 Framework

This article has the goal to start you the world of OOP PHP programming using the framework Yii version 2.

Motivation

This article is important for people who want to learn how to program in PHP with the correct way of using OOP while having a clean code using MVC design pattern. To achieve this goal, we are going to use Yii framework version 2 (Yii2), which is a high performance framework that has been highly used on the market. Yii2 framework enables you to build from simple to very complex web systems. It helps you to deliver more in less time, saving you a lot of time with its automation tool called Gii.

Stating engines

Yii2 is a complete PHP high performance framework, but it is not part of PHP itself; so, we need to download the template in order to start a new project with Yii2. Said that, you need to know that we can use it in two flavors: basic template and advanced template. Each one of these templates are meant to be used in specific systems and needs. As we are learning how to work with Yii2 framework, we will be using basic template.

  • Basic template is the best choice to the majority of projects, because it enables you to use all modern characteristics of web sites in a really simple way.
  • Advanced template is the best choice when you are developing complex web applications. Its structure is based on three internal applications: front-end, back-end and console. These applications help when developing and deploying in different environments.

Installing the basic template

There are two ways of installing templates in Yii2: 1) downloading the archive file (.tgz) directly; 2) using Composer to install. We are going to use the second option.

In order to install Yii2 framework, you need to have Apache (or another web server), PHP itself and Composer already installed on your system. We are not covering these installations here because there are many ways of achieving it depending on your system. Said that, let’s go to the use of Composer to install the basic template. Just use the command line below in your web server documents directory (frequently apache/htdocs):

php composer.phar create-project yiisoft/yii2-app-basic first_yii2 2.0.9

You must change first_yii2 in this command line for your own project name before running it.

When all download is done, your basic project is ready to use. If you have placed in the web server documents directory and your server is running, you could open your favorite web browser and access the site just created using the url http://localhost/first_yii2/web. You should see the same page in Figure 1.


Figure 1. Starting site with basic template

Basic template

Now that you have installed the basic template, you are able learn what is the purpose of the directories that were created for you. But here we are going to show you the most important and used ones.

  • Assets: Configuration of how assets, like images and CSS files, are loaded in each page;
  • Config: All configurations are here. Eg: global site configuration and database connection;
  • Controllers: All controllers are located here;
  • Vendor: All thirty party library that you have installed should be here. Composer do it automatically for you;
  • Views: This folder contains all views;
  • Web: All resources (as images, css, javascript and fonts) loaded in pages are located here.

Knowing MVC design pattern

MVC is a very popular design pattern that allows us to separate concerns and obtain a cleaner code. The application is separated in three parts: Model, View and Controller. Where:

  • Model: It is a set of classes that access data, as database, directly. Here are located all the data logic used in the application. In Yii2, there are two types of model objects: Forms (not connected to database, only to views) and Data Connected (based on ActiveRecord design pattern). They work together to achieve the site’s goals;
  • View: It is a set of HTML pages to be rendered in the web browser to the user. These pages are not accessed directly to the web browser. In Yii2, we use a set of PHP, Yii2 widgets and helpers to embed functionality into pages;
  • Controller: It is a set of classes that controls the flow of the site. These classes are the entry point of functionality into the site and could or not render a View.

The relationship of the MVC parts are shown in Figure 2.


Figure 2. MVC relationship

Creating a controller

Here we are going to learn how to create a basic page from ground up. This page is really simple because the goal here is to show you how is the simplest process of creating a page. After you learn that, you could deep your knowledge much simply.

Let’s create a file named FirstController.php into controllers directory as in Listing 1:

<?php
namespace app\controllers;

use yii\web\Controller;

class FirstController extends Controller {
    public function actionIndex() {
        return $this->render('index');
    }
}
Listing 1. Controller class.

Creating a view

Now, let’s create into views directory a new folder called first and a file named index.php inside it as in Listing 2:

<?php
use yii\helpers\Html;

$this->title = 'First web page in Yii2';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-login">
    <h1><?= Html::encode($this->title) ?></h1>

    <p>My first page written in Yii2.</p>
</div>
Listing 2. Index view.

Now, you can just access the page as in Figure 3.


Figure 3. First web page

With this, you know the bases of Yii2 framework! Now you can start developing your own web applications while exploring the other framework resources by yourself. Don’t be shy and do whatever you want with this amazing PHP framework. Let’s rock!

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login