How to create custom Component in Yii2

Hi there, Today I would like to write a simple tutorial on creating custom Component in Yii2 and I will show you how to use created a component in your application.

Here are the simple steps, just follow the step at the end you will have your first custom component.

Make a folder named “components” in your project root folder.

Crate a file with desired component name inside components folder and copy past the below code. Example – MyComponent.php

Add your component inside the config/web.php file


You are done!. Now you can use your component method “welcome” inside any of your app controller actions.


I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.