Category: Yii

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

  • Rules of URL

    A URL rule is an instance if yii\web\UrlRule. The urlManager components uses the URL rules declared in its rules property when the pretty URL format is enabled. To parse a request, the URL manager obtains the rules in the order they are declared and looks for the first rule. Step 1 − Modify the urlManager component in the config/web.php file. Step 2 − Go to your…

  • URL Routing

    To change the default route of the application, you should configure the defaultRoute property. Step 1 − Modify the config/web.php file in the following way. Step 2 − Got to http://localhost:8080/index.php. You will see the default contact page. To put your application in maintenance mode temporarily, you should configure the yii\web\Application::$catchAll property. Step 3 − Add the following function to the SiteController. Step 4 − Then, modify the config/web.php file in…

  • URL Formats

    When a Yii application processes a requested URL, first, it parses the URL into a route. Then, to handle the request, this route is used to instantiate the corresponding controller action. This process is called routing. The reverse process is called URL creation. The urlManager application component is responsible for routing and URL creation. It provides two methods…

  • Responses

    When a web application handles a request, it generates a response object, which contains HTTP headers, body, and HTTP status code. In most cases, you will use the response application component. By default, it is an instance of yii\web\Response. To manage response HTTP status codes, use the yii\web\Response::$statusCode property. The default value of yii\web\Response::$statusCode is 200. Step 1 − Add a…

  • HTTP Requests

    Requests are represented by the yii\web\Request object, which provides information about HTTP headers, request parameters, cookies, and so forth. The methods get() and post() return request parameters of the request component. Example − Step 1 − Add an actionTestGet function to the SiteController of the basic application template. Step 2 − Now go to http://localhost:8080/index.php?r=site/testget&id=1&name=tutorialspoint&message=welcome, you will see the following. To retrieve parameters of other request methods (PATCH, DELETE,…