Author: Awais Farooq
-
Behaviors
Behaviors are instances of the yii\base\Behavior class. A behavior injects its methods and properties to the component it is attached to. Behaviors can also respond to the events triggered by the component. Step 1 − To define a behavior, extend the yii\base\Behavior class. The above code defines the behavior with one property (prop1) and one method (myFunction). When…
-
Creating Event
In this chapter we will see to create an event in Yii. To show events in action, we need data. Preparing the DB Step 1 − Create a new database. Database can be prepared in the following two ways. Step 2 − Configure the database connection in the config/db.php file. The following configuration is for the system used currently.…
-
Events
You can use events to inject custom code at certain execution points. You can attach custom code to an event, and when the event is triggered, the code gets executed. For example, a logger object may trigger a userRegistered event when a new user registers on your web site. If a class needs to trigger events, you should extend…
-
GridView Widget
The GridView widget takes data from a data provider and presents data in the form of a table. Each row of the table represents a single data item, and a column represents an attribute of the item. Step 1 − Modify the datawidget view this way. Step 2 − Go to http://localhost:8080/index.php?r=site/data-widget, you will see a typical usage of the…
-
ListView Widget
The ListView widget uses a data provider to display data. Each model is rendered using the specified view file. Step 1 − Modify the actionDataWidget() method this way. In the above code, we create a data provider and pass it to the datawidget view. Step 2 − Modify the datawidget view file this way. We render the ListView widget.…
-
Data Widgets
Yii provides a set of widgets for displaying data. You can use the DetailView widget to display a single record. The ListView widget, as well as Grid View, can be used to display a table of records with features like filtering, sorting, and pagination. Preparing the DB Step 1 − Create a new database. Database can…
-
Data Providers
Yii provides a set of data provider classes that encapsulate pagination and sorting. A data provider implements yii\data\DataProviderInterface. It supports retrieving sorted and paginated data. Data providers usually work with data widgets. Yii includes − You define the sorting and pagination behaviors of a data-provider by configuring its pagination and sort properties. Data widgets, such as yii\grid\GridView, have a property…
-
Properties
Class member variables in PHP are also called properties. They represent the state of class instance. Yii introduces a class called yii\base\Object. It supports defining properties via getter or setter class methods. A getter method starts with the word get. A setter method starts with set. You can use properties defined by getters and setters like class member variables. When a property is…
-
Sorting
When displaying lots of data, we often need to sort the data. Yii uses an yii\data\Sort object to represent a sorting schema. To show sorting in action, we need data. Preparing the DB Step 1 − Create a new database. Database can be prepared in the following two ways. Step 2 − Configure the database connection in the config/db.php file. The…
-
Pagination
When you have too much data to display on a single page, you should display it on multiple pages. This is also known as pagination. To show pagination in action, we need data. Preparing the DB Step 1 − Create a new database. Database can be prepared in the following two ways. Step 2 − Configure the…