Author: Awais Farooq
-
Authorization
The process of verifying that a user has enough permission to do something is called authorization. Yii provides an ACF (Access Control Filter), an authorization method implemented as yii\filters\AccessControl. Modify the behaviors() function of the SiteController − In the above code, ACF is attached as a behavior. The only property specifies that the ACF should be applied…
-
Authentication
The process of verifying the identity of a user is called authentication. It usually uses a username and a password to judge whether the user is one who he claims as. To use the Yii authentication framework, you need to − The basic application template comes with a built-in authentication system. It uses the user application…
-
Error Handling
Yii includes a built-in error handler. The Yii error handler does the following − To disable the error handler, you should define the YII_ENABLE_ERROR_HANDLER constant to be false in the entry script. The error handler is registered as an application component. Step 1 − You can configure it in the following way. The above configuration sets…
-
Logging
Yii provides a highly customizable and extensible framework. With the help of this framework, you can easily log various types of messages. To log a message, you should call one of the following methods − The above methods record log messages at various categories. They share the following function signature − where − A simple…
-
Aliases
Aliases help you not to hard-code absolute paths or URLs in your project. An alias starts with the @ character. To define an alias you should call the Yii::setAlias() method − You can also derive a new alias from an existing one − You can call the Yii::setAlias() method in the entry script or in a writable…
-
Fragment Caching
Fragment caching provides caching of a fragment of a web page. Step 1 − Add a new function called actionFragmentCaching() to the SiteController. In the above code, we created a new user and displayed a cachedview view file. Step 2 − Now, create a new file called cachedview.php in the views/site folder. We have enclosed a content generation logic in a pair of beginCache() and…
-
Caching
Caching is an effective way to improve the performance of your application. Caching mechanisms store static data in cache and get it from cache when requested. On the server side, you may use cache to store basic data, such as a list of most recent news. You can also store page fragments or whole web…
-
Testing
When we write a PHP class, we debug it step by step or use die or echo statements to verify how it works. If we develop a web application, we are entering test data in forms to ensure the page works as we expected. This test process can be automated. Automatic test approach makes sense…
-
Fields
y overriding fields() and extraFields() methods, you can define what data can be put into a response. The difference between these two methods is that the former defines the default set of fields, which should be included in the response while the latter defines additional fields, which may be included in the response if an end user…
-
RESTful APIs in Action
The controller class extends from the yii\rest\ActiveController class, which implements common RESTful actions. We specify the $modelClass property so that the controller knows which model to use for manipulating data. Step 1 − Create a file called UserController.php inside the controllers folder. Next we need to set up the urlManager component, so that the user data can be accessed and manipulated with…