Yii Framework Tutorial for Beginners

What is Yii?

The Yii is a light-weighted, open-source PHP based framework which develops web application faster. It is used to accelerate web development applications quickly. It is a component-based, high-performance PHP framework for building large scale applications. This framework is built around the Model-View-Controller composite pattern. It includes an in-built code generator called Gii, which writes the code for you and save your time. The Yii is a Chinese name which means Yes it is. It is proper for every sized project giving a highly optimized performance. Yii Framework has its benefits, and this is the reason for which it is placed at the top spot among the other similar frameworks. Yii even represents the next generation of the PHP frameworks. Moreover, Yii is a supportive wide range of wizards that are used for conducting common tasks such as CRUDing (Creating, Reading, Updating and Deleting). This is not something that we find in any other PHP frameworks.

History of Yii

QiangXue initiated the Yij framework on January 1, 2008. This framework was developed to handle the slow processing of complex pages, complicated customization of many controls and steep learning curve. The Yii 1.0 version was released for the public on December 3, 2008, after almost one year's development. On October 2014, the complete rewrite of Yii 2.0.0 was released by keeping the earlier simplicity and feasibility while implementing the latest technologies to make it better.

Features of Yii Framework

Yii Framework provides secure and professional features to create robust projects rapidly. Some of the salient features of Yii are discussed below: Easy Installation: Yii Framework can be downloaded either using Composer method or manually but developers prefer to use the Composer method as it saves a lot of time. Use Modern Technologies: Yii framework is completely based on OOPS concepts but also uses some advanced features of PHP. All classes are namespaced in Yii, which means it allows taking advantage of their autoloader. Extensible: Yii framework is highly extensible as it allows its users to use third-parties libraries by installing them with Composer and then after configuring includes them in an autoloader. Testing: Yii2 is integrated with Codecepetion. Codeception is a PHP testing framework that simplifies the process of creating the unit, functional and acceptance tests for your application. Codeception facilitates the testing configuration of an application in a simple manner. Inbuilt Security: Security is an essential feature for an application and Yii provides excellent functionality for security. It has several security components that have several methods which help to create a more secure application. It is equipped with many security measures to prevent external attacks. Less Time for Development: To develop any application, many tasks are repeated more than once. Hence, increasing the line of code and making the work tedious. But, Yii framework provides some tools that help to reduce your time on this kind of functions. One of the tools is Gii, which quickly creates a web template for:
  • CRUD
  • Models
  • Controllers
  • Extensions
  • Forms
AJAX-enabled widgets: Yii provides a set of AJAX-enabled widgets, such as auto-complete input field, tree view and data grid. Thus, creating the writing of highly efficient and versatile user interface extremely simple. Performance: Yii developers have acknowledged that their primary goal was to achieve high performance. It provides several tools to enhance the speed of application. Yii offers a higher rate for receiving request per second and has less peak memory usage. MVC Pattern: This framework is built around the Model-View-Controller composite pattern to separate the code. Modeling Database: Database can be modeled in terms of objects instead of long SQL statements. Error Handling and Logging: In this framework, the correction with errors are being handled and presented more nicely, with all the log messages accurately categorized and routed to different destinations. Form Input and Validation: It has a set of validators, widgets and numerous helper methods which simplifies the task and helps in collecting the form input and thereafter, validating it. Automatic Code Generation: It provides a set of intuitive and highly extensible code generation tools that will generate the code for you very quickly such as CRUD and form input. Detailed Documentation: Yii Framework provides complete documentation stating every method or property. Skinning and Themeing: It provides skinning and themeing mechanism that permits you to switch outlook of a Yii generated website quickly.

Dis-advantages of Yii Framework

Every coin has two sides. Every technology comes with certain difficulties. The disadvantages of Yii are the following:
  • The Ajax features are not well-drafted in Yii and also require the help of JavaScript whenever needed.
  • It does not provide AR queries for building up of multiple relations.
  • This framework depends on static methods. Hence, because of this reason, many developers go against the framework.
  • It requires previous knowledge on PHP programming language and other add-ons. This becomes difficult for the beginner programmers to master this.

Yii Structure

Yii2 Framework has a new and improved application structure. It uses Composer to manage its dependencies. Yii1 has only a basic application structure whereas Yii2 has basic as well as advanced application structure. Yii2 divides the entire application into the following sections:
  • Backend: For backend web development
  • Common: Include standard files for all the application
  • Console: Different console application
  • Environments: Simple environment configurations
  • Frontend: For frontend web development
Yii2's application structure is precise and clear. It contains the below-given folders ?
  • Assets–The Assets folder includes all .jsand .css files placed on the web page.
  • Commands? This folder contains the controllers that can be used from the terminal itself.
  • Config– The Config folder include various config files for managing database, application and application parameters.
  • Mail? This folder holds the mail layout.
  • Models? This folder contains the models used in the application.
  • Runtime? This folder is used for storing the runtime data.
  • Tests? This folder includes all the tests, i.e., acceptance, unit or functional.
  • Vendor– The Vendor folder contains all the third-party packages managed by the Composer.
  • Views? This folder is for views, that are displayed by the controllers.
  • Layout- The layoutfolder is used for the page template.
  • Web? The entry point from the web.
Yii Structure

Above image Is only for reference.

Installation of Yii Framework

The easiest way to get started with Yii2 is to use the elementary application template provided by the Yii2 team. The same template is also available through the Composer tool. Step 1 –Firstly, download the Composer PHAR by using the following command. This command installs the composer asset plugin, which manages the npm and bower dependencies. curl -sS https://getcomposer.org/installer | PHP Step 2 –Once the download is completed, move this archive to the bin directory. This command installs Yii2 basic application template in a directory called myfirstapp. mvcomposer.phar /usr/local/bin/composer Step 3 –Once the Composer is being installed, you can install the Yii2 basic application template by running these commands. composer global require "fxp/composer-asset-plugin:~1.1.1" composer create-project --prefer-distyiisoft/yii2-directory myfirstapp Step 4 – Now, open the myfirstapp directory and launch the web server built into PHP. php -S localhost:8080 -t web Step 5 ? Then open http://localhost:8080 in your browser. You will see the welcome page.