Introduction to Yii2
The Yii project had started on January 1, 2008, by some of the developers that were responsible for the PRADO framework. Their vision was to improve upon the PRADO framework and address all of it’s shortcomings. Yii 1.0 was formally released on December 3, 2008, and quickly became a major force in the huge ocean of PHP frameworks due to its performance, ease of use and adherence to some of the best practices of the industry at that time.
Over time, as development on the PHP language itself picked up steam and new and more modern features got added, the developers of Yii started to think about a newer, complete rewrite of the framework. This rewrite would make extensive use of these advanced features (such as namespaces, anonymous functions, etc.). So, the development of Yii2 started which would, at a minimum, support PHP 5.4+ (Yii supported PHP 5.1+). The developers made the first commit on April of 2011.
I thought it would be a good time to take a look under the hood of this cutting edge PHP framework. I will take a detailed look at what Yii2 offers developers in terms of modern features and standard practices in this and other following posts . Developers that have experience developing in Yii and who would like to know what changes are expected in Yii2 will find these posts helpful.
The most obvious change that you will come across in Yii2 is the use of namespaces. PHP 5.3 introduces the concept of namespaces to the PHP world. As a developer, you will find that every Yii2 core class is now namespaced (e.g.:
yii\web\Request). The “C” prefix that were used in class names in Yii 1.1 are now history because of Namespaces. Namespaces also avoids the chance of name collisions while allowing shorter class names and in the process improving the overall readability and maintainability of the code.
Object and Component Class
The CComponent class of Yii 1.1 has been broken down into two classes:
\yii\base\Component. Object is the base class you will use for creating and managing custom data structures. Objects have a uniform way of being configured and thus can be initialized using a configuration array. Something like the following can be used to initialize any Object:
$object = Yii::createObject([
'class' => 'MyClass',
'property1' => 'abc',
'property2' => 'cde',
], [$param1, $param2]);
Components are Objects with additional features like Event Handling and Behaviors. Event Handling is much more robust in Yii2. Now there is no need to define an “on” prefixed method. Instead to attach a handler to an event, you should now use the
on method, like so:
$component->on($eventName, $handler, $params);
Events are very useful both to make your components flexible and to hook into framework and extensions workflow. Events can now be triggered using the trigger method:
Behaviors (known in some other languages as mixins) are used in order to enhance the functionality of existing components without modifying its code. Behaviors can be added either by extending the
behaviors() method of the Component or by using the
Yii2 expands the usage of path aliases to both file/directory paths and URLs. An alias must start with an
@ symbol so that it can be differentiated from file/directory paths and URLs. For example, the alias
@yii refers to the Yii installation directory while
@web contains the base URL for the currently running web application. The Yii core code supports path aliases in most places.
Aliases predefined by the core framework are as below:
- @yii – framework directory.
- @app – base path of currently running application.
- @runtime – runtime directory.
- @vendor – Composer vendor directory.
- @webroot – web root directory of currently running web application.
- @web – base URL of currently running web application.
Helper classes typically contain static methods only and are used as follows:
echo Html::encode('Test > test');
There are several classes provided by framework:
Those are some of the very basics of the Yii2 framework which lay the foundation for bringing the latest and cutting-edge concepts in framework design to the Yii platform. In the next post, I will look in detail at the MVC concepts and how they have changed in the new version of this popular framework. This blog post series will continue to look into the various features and functionality of Yii2. So stick around…
Navigate to other parts of the series:
Note: This post was moved here from my old blog…