Behavior

Caching in Yii2 and behaviors
Caching in Yii2 and behaviors

What should you do if you need to implement standard functionality (such as data caching) for some default models? In my case, behavior and its powerful capabilities came to the rescue.

In short, behavior in the Yii2 framework allows you to extend controllers and models (or anything inherited from the “Component” class) with additional methods. It works similarly to PHP traits but is not the same. If you’re interested in a more detailed explanation, check the official Yii documentation.

In this article, I’ll describe my experience combining caching, models, and behaviors.

Story: At a certain point in development, I needed to implement caching for four default models:

  • Rubrics
  • Categories
  • Regions
  • Cities

read more...