Author: Awais Farooq

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

  • Sessions

    Sessions make data accessible across various pages. A session creates a file on the server in a temporary directory where all session variables are stored. This data is available to all the pages of your web site during the visit of that particular user. When a session starts, the following happens − To start a…

  • AJAX Validation

    The username validation should only be done on the server side because only the server has the needed information. In this case, you can use AJAX-based validation. Step 1 − To enable the AJAX validation, modify the registration view this way. We should also prepare the server, so that it can handle the AJAX requests. Step 2 − Modify…

  • Ad Hoc Validation

    Sometimes you need to validate values that are not bound to any model. You can use the yii\base\DynamicModel class, which supports defining both attributes and rules on the fly. Step 1 − Add the actionAdHocValidation method to the SiteController. In the above code, we define a “dynamic” model with username and email attributes and validate them. Step 2 − Type http://localhost:8080/index.php?r=site/ad-hoc-validation in the address bar of…

  • Validation

    You should never trust the data received from users. To validate a model with user inputs, you should call yii\base\Model::validate() method. It returns a Boolean value if the validation succeeds. If there are errors, you may get them from the yii\base\Model::$errors property. Using Rules To make the validate() function work, you should override the yii\base\Model::rules() method. Step 1 − The rules() method returns an array in…

  • HTML Forms

    When a form is based upon a model, the common way of creating this form in Yii is via the yii\widgets\ActiveForm class. In most cases, a form has a corresponding model which is used for data validation. If the model represents data from a database, then the model should be derived from the ActiveRecord class. If the model captures…