Wednesday, March 22, 2017

Yii - 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 for long term projects, which are −

Yii - 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 pages. On the client side, you can use HTTP caching to keep most recently visited pages in the browser cache.

Yii - Fragment Caching

Fragment caching provides caching of a fragment of a web page.
Step 1 − Add a new function called actionFragmentCaching() to the SiteController.
public function actionFragmentCaching() {
   $user = new MyUser();
   $user->name = "cached user name";

Yii - 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 −
// an alias of a file path
Yii::setAlias('@alias', '/path/to/alias');
// an alias of a URL

Yii - 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 −
  • Yii::error() − Records a fatal error message.
  • Yii::warning() − Records a warning message.
  • Yii::info() − Records a message with some useful information.
  • Yii::trace() − Records a message to trace how a piece of code runs.

Yii - Error Handling

Yii includes a built-in error handler. The Yii error handler does the following −
  • Converts all non-fatal PHP errors into catchable exceptions.
  • Displays all errors and exceptions with a detailed call stack.
  • Supports different error formats.
  • Supports using a controller action to display errors.

Yii - 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 −

Yii - 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 −

Yii - Localization

I18N (Internationalization) is the process of designing an application that can be adapted to various languages. Yii offers a full spectrum of I18N features.
Locale is a set of parameters that specify a user's language and country. For example, the en-US stands for the English locale and the United States.

Yii - Gii

Gii is the extension, that provides a web-based code generator for generating models, forms, modules, CRUD, and so forth.
By default, the following generators are available −
  • Model Generator − Generates an ActiveRecord class for the specified database table.
  • CRUD Generator − Generates a controller and views that implement CRUD (Create, Read, Update, Delete) operations for the specified model.

Gii - Creating a Model

To create a Model in Gii −
<?php
   namespace app\models;
   use app\components\UppercaseBehavior;
   use Yii;
   /**

Gii - Generating Controller

Let us see how to generate a Controller.
Step 1 − To generate a controller with several actions, open the controller generator interface fill in the form.