The Definitive Guide to Yii 2.0

The Definitive Guide to Yii 2.0

The Definitive Guide to Yii 2.0

This tutorial is released under the Terms of Yii Documentation.

All Rights Reserved.

2014 (c) Yii Software LLC.



Events in Yii 2
Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

Events

Events allow you to inject custom code into existing code at certain execution points. You can attach custom code to an event so that when the event is triggered, the code gets executed automatically. For example, a mailer object may trigger a messageSent event when it successfully sends a message. If you want to keep track of the messages that are successfully sent, you could then simply attach the tracking code to the messageSent event.

Properties in Yii 2
Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

Properties

In PHP, class member variables are also called properties. These variables are part of the class definition, and are used to represent the state of a class instance (i.e., to differentiate one instance of the class from another). In practice, you may often want to handle the reading or writing of properties in special ways. For example, you may want to always trim a string when it is being assigned to a label property. You could use the following code to achieve this task:

$object->label = trim($label);

Components Yii 2
Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

Components

Components are the main building blocks of Yii applications. Components are instances of [[yii\base\Component]], or an extended class. The three main features that components provide to other classes are:

Yii logging framework
Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

Logging

Yii provides a powerful logging framework that is highly customizable and extensible. Using this framework, you can easily log various types of messages, filter them, and gather them at different targets, such as files, databases, emails.

Using the Yii logging framework involves the following steps:

Yii Handling Errors
Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

Handling Errors

Yii includes a built-in [[yii\web\ErrorHandler|error handler]] which makes error handling a much more pleasant experience than before. In particular, the Yii error handler does the following to improve error handling:

  • All non-fatal PHP errors (e.g. warnings, notices) are converted into catchable exceptions.
  • Exceptions and fatal PHP errors are displayed with detailed call stack information and source code lines in debug mode.
  • Supports using a dedicated controller action to display errors.
  • Supports different error response formats.

Sessions and Cookies in Yii 2
Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

Sessions and Cookies

Sessions and cookies allow data to be persisted across multiple user requests. In plain PHP you may access them through the global variables $_SESSION and $_COOKIE, respectively. Yii encapsulates sessions and cookies as objects and thus allows you to access them in an object-oriented fashion with additional useful enhancements.

Subcategories