Tutorial

Yii2 – Understanding in Detail

Choosing a good framework for your project is always a tough decision, mainly when you have too many choices. Same situation we have for choosing a framework for PHP based web development projects. We have to consider lots many aspects before selecting any frameworks.

After working with various PHP frameworks (CodeIgniter, CakePHP), I have decided to give a try to Yii and it feels amazing because of various features. After using it for a long time and for various projects, I feel confident using this framework for any project.

Yii2 - What's New

As I am having good exposure to Yii2, I have decided to share the same to my readers. Sounds interesting? Hope you find this series of articles helpful for your learning curve.

In this article series, I will start with basics things and gradually we will move to advance things. In this very first article, we will see some of the features which are really good. Along with those, I will be covering some of the things which have been changed from the previous version of Yii.

Good Features

Utilizing Modern Features

Yii is developed using pure OOP approaches by implementing various Design Patterns. It uses some of the advanced features PHP including anonymous functions, late static binding, interfaces and SPL classes.

[gads]

Every class in Yii structured using namespaces so it can take advantage of the PSR-4 compliant autoloader.

Extensible

Yii is developed in such a way that you can extend each and every part of a base framework. We will see this topic in much detail in upcoming articles.

Gii – Code Generation Tool for Yii

We have few things which are repeated in every project we work on. Yii’s goal was to automate repeating tasks and let programmer focus on pure business logic for a project and that’s where Gii comes into the picture.

Yes, Gii is code generation tool for Yii and it works as a module of Yii. Gii helps us to generate Models, Controllers, Whole CRUD part, extensions and module. It’s just a matter of adding few lines in your config file and you are done, Gii is ready to use.

[gads]

In one of the article of this series, we will see how to deal with Gii in detail, which involved installation and how to use part.

Easy Testing

Codeception is a PHP testing framework that helps simplify the process of creating various tests like unit, functional and acceptance for your application. Yii2 is tightly coupled with Codeception.

Changes from Older Version

PHP 5.4 Support

PHP 5.4 is minimum requirement for Yii2, so we can utilize many of the features of PHP 5.4 as below:

We can use shorthand array syntax for arrays now:

[cc lang=”php”]

// Yii 1.1
$array = array(1,2,3);

// Yii 2
$array = []1,2,3];
[/cc]

PHP short tags are also available to use:

[cc lang=”php”]

// Yii 1.1

// Yii 2

[/cc]

$app Object

With Yii 1.1, we had access to main app object as a method of Yii class but with Yii 2 we can access the same object as a property of the Yii class.

[cc lang=”php”]
// Yii 1.1
Yii::app();

// Yii 2
Yii::$app;
[/cc]

Views

In Yii 1.1 if you are in view file then you can get access to an object of a controller using [code]$this[/code] but this is changed a lot in Yii2. With Yii2 if you are in view and try to access [code]$this[/code] you will get an object of the class yii\web\View while an object of a controller can be accessed using [code]$this->context[/code].

There are lot many other things which are changed in Yii2, but it’s really hard to cover all those in just one article. So I will cover this topic in various articles of this series. I will try to compare things with older version while discussing a feature of Yii2.

Up Next

In next article, we will start with the installation process of Yii2.

Please do share your comments below or if you have any specific request for any article in this series, do let me know via comment. I would be happy to include that in this series.

Shares:

Leave a Reply

Your email address will not be published. Required fields are marked *