Yii2 – What you need to know – Part IV (Events, Behaviors & Errors)

Events, Behaviors, Errors

In this installment of the series where we are exploring the Yii2 framework, we will look in detail at the Events, Behaviors and Error Handling features of the Yii2 framework. There are some notable differences in the way Yii2 handles these critical features when compared to how Yii used to handle them.


For those of you that never used events in your previous Yii based projects, let me tell you, you have been missing a very powerful feature. Events are very useful for two reasons. First, they can make your components more flexible. Second, you can hook your own code into the regular workflow of both the framework and the extensions in use.

You can assign event handlers within your code using the on method of a component object. The first argument is the name of the event we are watching out for and the second is the function to be called when that event occurs:

The handler can be any of the following:

  • The name of a global function
  • An array consisting of a model name and method name
  • An array consisting of an object and a method name
  • An anonymous function

Examples of these:

In addition, event handlers can also be attached within configuration files also.

Most events will be triggered through the normal workflow. For example, the “beforeSave” event occurs before an Active Record model is saved.

But you can also manually trigger an event using the trigger method, invoked on the component with the attached event handler:

A relatively newer event management feature is adding event handlers to all instances of a class at one go.


A behavior implements the Mixin pattern in the Yii framework. These  can be used to enhance the functionality of an existing component without modifying the component’s code. A behavior can also respond to events triggered in the component, thus intercepting the normal code execution. Unlike PHP’s traits, behaviors can be attached to classes at runtime.

We use the behaviors() method of a component to attach behaviors to it. The timestamp behavior of Yii2 is a great example of the utility of behaviors:

In the above, the name timestamp can be used to reference the behavior through the component. For example, $user->timestamp gives the attached timestamp behavior instance. The corresponding array is the configuration used to create the TimestampBehavior object.

Besides responding to the insertion and update events of ActiveRecord, TimestampBehavior also provides a method touch() that can assign the current timestamp to a specified attribute. As aforementioned, you can access this method directly through the component, like the following:

You can also create your own behaviors. As an example:

This can then be accessed in your class:

Error Handling

Yii error handling was always different from handling errors using plain PHP. Yii convers all non-fatal errors to exceptions.

As demonstrated above you may handle errors using try-catch.

The exception object has the following properties:

  • statusCode: the HTTP status code (e.g. 403, 500). Available for HTTP exceptions only.
  • code: the code of the exception.
  • message: the error message.
  • file: the name of the PHP script file where the error occurs.
  • line: the line number of the code where the error occurs.
  • trace: the call stack of the error.


As can be seen, there are some notable differences in the way Yii2 handles Events and Behaviors. Hope you enjoyed this installment in the series. Please feel free to provide your feedback and thoughts.

Navigate to other parts in this series:

Part IPart IIPart III and Part V

Note: Post was moved here from my old blog…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.