Category: Yii

  • 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…

  • Formatting

    To display data in a readable format, you can use the formatter application component. Step1 − Add the actionFormatter method to the SiteController. In the above code, we just render the formatter view. Step 2 − Now, create a formatter.php view file inside the views/site folder. Step 3 − Go to http://localhost:8080/index.php?r=site/formatter, you will see the following output. The formatter component supports the following formats related with date and time −…

  • Files Upload

    You can easily implement a file uploading function with the help of yii\web\UploadedFile, models and yii\widgets\ActiveForm. Create a directory ‘uploads’ in the root folder. This directory will hold all of the uploaded images. To upload a single file, you need to create a model and an attribute of the model for uploaded file instance. You should also validate the file…

  • Using Cookies

    Cookies allow data to be persisted across requests. In PHP, you may access them through the $_COOKIE variable. Yii represents cookie as an object of the yii\web\Cookie class. In this chapter, we describe several methods for reading cookies. Step 1 − Create an actionReadCookies method in the SiteController. Step 2 − To see sending cookies in action, create a method called actionSendCookies in the SiteController. Step 3 −…

  • Cookies

    Cookies are plain text files stored on the client side. You can use them for tracking purpose. There are three steps to identify a returning user − Cookies are usually set in an HTTP header as shown in the following code. PHP provides the setcookie() function to set cookies − where − To access cookies in PHP,…

  • Using Flash Data

    Yii provides a concept of flash data. Flash data is a session data which − Step 1 − Add an actionShowFlash method to the SiteController. Step 2 − Inside the views/site folder, create a View file called showflash.php. Step 3 − When you type http://localhost:8080/index.php?r=site/show-flash in the address bar of the web browser, you will see the following. Yii also provides the following session…