-
Book Overview & Buying
-
Table Of Contents
Yii2 By Example
By :
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 of the project.
Let's see how to create a simple custom component:
namespace app\components;
use Yii;
use yii\base\Component;
class MyComponent extends Component
{
..
..
}We can instantiate this component as follows:
$myCmp = new \app\components...
Change the font size
Change margin width
Change background colour