Register and activate modules

With the registration.php and module.xml files created within our new Macademy BestSelling module, our module contains everything it needs to be registered and made visible to Magento.

Magento comes with a built-in command line tool which you'll use to carry out many different tasks within Magento, a common one being enabling and disabling modules.

Let's go ahead and type bin/magento to see all of the available commands. We can take a quick look around to see everything that is available for us.

Feel free to check out all of these commands and read up on what's available, but at this time the commands we are looking for are located within the module grouping, specifically the module status and module enable command.

First let's type:

bin/magento module:status

This command displays the enable or disable status of all modules. We should see the Macademy_BestSelling module in this list under disabled modules. If you don't see it here, double check your vendor and module folder names for spelling mistakes or casing inconsistencies, then do the same for the registration.php and module.xml files and their contents. Once everything is spelled and cased correctly, the module should show up when checking the status.

Next let's enable the module by typing bin/magento followed by module:enable. Next comes the module name. We'll use the Vendor name underscore Module name format, so this would be Macademy underscore BestSelling.

bin/magento module:enable Macademy_BestSelling

You should see output that says the module has been enabled. If in the module status step you receive an "Unknown module" error here, you'll need to again check your module for spelling and casing issues.

At this point our module has been enabled. We'll see this message "To make sure that the enabled modules are properly registered, run 'setup:upgrade'.". You don't need to run this command unless your module contains database schema or update scripts.

The module:enable command is really a helper for enabling modules. Even though it's not recommend, we can also enable modules manually.

Open up app/etc/config.php. This file simply returns an array of data, however it is a crucial file for Magento to operate. This file just contains modules now, but it can also contain overrides for other configuration values. If a module is in this array and has a 1, it's enabled. If it has a 0, it's disabled. And if it isn't in this list, Magento isn't aware of the module.

You can manually trigger modules on and off by setting them to 1 and 0 in this file if you need to. But that really isn't recommend because there is some magic going on behind the scenes with the module:enable command.

Like all things which appear simple, this really isn't that simple. When you run module:enable, it does a bit more under the hood. This includes checking whether the module you are enabling depends on other modules, checking whether those modules are enabled, then checks whether the dependencies of those dependent modules are enabled, and so on up the tree. It also uses that sequence node from module.xml files to determine exactly how to order modules within the config.php file. So generally, it's much safer to just use the command line when enabling or disabling modules to keep items within the config.php file in the proper order.

Discussion

4 comments