My First Craft CMS Module

Adding a Service to a Craft Module, Part 1

We start out with our service by adding a new service class file.

In Craft, a ser­vice is defined as the class file where you han­dle busi­ness log­ic”. The ser­vice class­es are attached to the mod­ule class as a Yii com­po­nent. In a refined plu­g­in or mod­ule, a ser­vice class is how your mod­ule or oth­er code can inter­act with the func­tion­al­i­ty of the module.

Think about the ser­vice class as the pro­gram­mat­ic entry point to do some­thing in your mod­ule. You want to add CP nav items, you do access a method in the ser­vice class to do that. Yes, we do need to reg­is­ter the events in the init() method still but this is a spe­cial case.

My First Craft CMS Module is made up of the following videos: