Yii 2.0 Benefits and Features

Yii 2.0 Development Company

Yii 2.0 Beta is a complete rewrite of Yii 1.1, the current stable version. The current version of Yii was heavily influenced by Ruby on Rails. It is a clean adaptation of the great features of RoR to the PHP fraternity. Yii 2.0 is a much comprehensive web development framework, aimed to take the framework to the next orbit. The team has clearly developed Yii 2.0 keeping Yii’s philosophy of adapting the best practices and features of other web frameworks. Some of the interesting features of what Yii 2.0, compared to Yii 1.1 is discussed in the sections below

Namespace

The foremost and very important change in Yii 2.0 is the use of Namespaces. All the core classes are now namespaced. The class names do not use the “C” prefix. The use of Namespace improves the way Yii codes are written avoiding or limiting ambiguity in class names, evading in-identifier grouping for hierarchy and naming conflicts. The Yii class loader lets the developers use any core class of Yii without explicitly including that class file.

Component and Object

The component class CComponent in Yii 1.1 is now broken into two classes – Object Class and Component Class. The basic data structures are provided in Object Class, configuration and the property feature. Component Class extends from the Object class adding events and behaviors. These way developers can now use Objects for classes that represent basic data structures while component is used for application components implementing higher logic with events and behaviors.

Object Configuration is simplified, introducing a uniform way of configuring objects. Any descendant class of Object should declare its constructor.

Events definition no longer needs an on- method. The developer is free to use any event names and attach a handler to the event. The handlers can also be attached with parameters to be used at a later stage, enabling the use of global events.

Path Alias is extended to both file/directory paths and URLs. It is also closely related with Namespaces.

View Yii 2.0 introduces a View class to represent the view part of the MVC pattern. It can be configured globally through the “view” application component. It is also accessible in any view file via $this. This is one of the biggest changes compared to 1.1: $this in a view file no longer refers to the controller or widget object. It refers to the view object that is used to render the view file. To access the controller or the widget object, you have to use $this->context now.

For partial views, the View class now includes a render () function. This creates another significant change in the usage of views compared to 1.1: $this->render (…) does not output the processed content; you must echo it yourself.

Because you can access the view object through the “view” application component, you can now render a view file anywhere in your code, not necessarily in controllers or widgets.

Also, there is no more CClientScript in Yii 2.0. The View class has taken over its role with significant improvements.

While Yii 2.0 continues to use PHP as its main template language, it comes with two official extensions adding support for two popular template engines: Smarty and Twig. The Prado template engine is no longer supported. To use these template engines, you just need to use tpl as the file extension for your Smarty views, or twig for Twig views. You may also configure theView::$renderers property to use other template engines.

Models A model is now associated with a form name returned by its formName() method. This is mainly used when using HTML forms to collect user inputs for a model. Previously in 1.1, this is usually hard coded as the class name of the model.

A new method called scenarios () is introduced. This method will declare which attributes require validation in which scenarios. This method also determines which attributes are safe and which are not. In particular, given a scenario, if an attribute appears in the corresponding attribute list in scenarios() and the name is not prefixed with, it is considered safe. Because of the above change, Yii 2.0 no longer has “unsafe” validator.

Widgets usage in Yii 2.0 is more straightforward. You mainly use the begin (), end () and widget () methods of the Widget class. This improves the IDE support.

Themes work completely different in 2.0. They are now based on a path map to “translate” a source view into a themed view. For example, if the path map for a theme is [‘/web/views’ => ‘/web/themes/basic’], then the themed version for a view file /web/views/site/index.php will be /web/themes/basic/site/index.php.

For this reason, theme can now be applied to any view file, even if a view rendered outside of the context of a controller or a widget.

There is no more CThemeManager. Instead, theme is a configurable property of the “view” application component.

Assets Yii 2.0 introduces a new concept called asset bundle. It is similar to script packages (managed by CClientScript) in 1.1, but with better support.

An asset bundle is a collection of asset files (e.g. JavaScript files, CSS files, image files, etc.) under a directory. Each asset bundle is represented as a class extending yii\web\AssetBundle. By registering an asset bundle via yii\web\AssetBundle::register(), you will be able to make the assets in that bundle accessible via Web, and the current page will automatically contain the references to the JavaScript and CSS files specified in that bundle.

Yii 2.0 has also introduced commonly used Static Helper classes, field concept for building forms using Active Forms and improvised the Query Builder. URL Management now supports optional parameters. The support for implementing RESTful Web Service APIs is enhanced in Yii 2.0

Active Record has undergone major overhaul. The relational ActiveRecord query is now done via getter methods that return an Active Query object. The model () method is now replaced with find () method when performing queries.

The above listed major changes to Yii 1.1 and other smaller nuances make Yii 2.0, clearly a platform of choice for PHP Web development. Truly the best practices and features across different platforms are considered and adapted to suit the needs of PHP Web development.

 

Related Posts

Leave a comment

Free Consulting