Creating components

A component is a reusable object that should contain only logic, and it is callable from every point of the app. In a component, we put all the functions that are usable in more than one place of the app.

Technically, a component extends yii\base\Component that implements the property, event and behavior features. We can have two kinds of component: component and application component. The only difference between them is that the second has to be also configured in the configuration file of the app in the components property and it is available as a property from the Yii::$app object. Examples of application components are db, user, and so on.

Usually, components are stored in the components folder starting from the root folder ...

Get Yii2 By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.