Let’s do this!

The aim here is just to get a module registered with Magento 2. Nothing fancy.

Assuming you now have a installation of Magento 2 (if not see Creating a Magento 2 project with Composer), the first job is to let it know about your new module. You can do this with just two files.

Firstly create your module structure; app/code/[codePool]/[Vendor]/[ModuleName]

Hang on one second! What is [Vendor] and [ModuleName]? [Vendor] is your unique name. It defines your namespace. In my case, I use “Smartie”. [ModuleName] is erm, well, your module name :) From here on in, anytime you see [Vendor] and [ModuleName], you should replace it with your details.

Create app/code/[codePool]/[Vendor]/[ModuleName]/etc/module.xml.

In module.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="[Vendor]_[ModuleName]" setup_version="2.0.0" />
</config>

Create app/etc/[Vendor]/[ModuleName]/registration.php.

In registration.php:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    '[Vendor]_[ModuleName]',
    __DIR__
);

Ok so we have our basic module set up with all the necessary files in place BUT, Magento is not magic. We still need to ask it to activate our module.

bin/magento module:enable [Vendor]_[ModuleName]

Great, Magento now knows about our module. In fact, if you look in app/etc/config.php, you should see your module in there. A bit like;

<?php
return array (
  'modules' => 
  array (
    ..
    [Vendor]_[ModuleName] => 1
    ..
  ),
);

After you ran the previous command, you may have noticed Magento asking you to run another command;

bin/magento setup:upgrade

That does a lot of things in the background but for now all we need to know is that it makes an entry in the setup_module table

There are a number of ways to check that your module is now registered with Magento. My preferred way is to run;

bin/magento module:status

Do that and you should see your new module appear in the list of enabled modules.


Composer

If we want to be able to install our module via composer later on, we need to create app/code/[codePool]/[Vendor]/[ModuleName]/composer.json

{
    "name": "[vendor]/[modulename]",
    "description": "",
    "type": "magento2-module",
    "version": "1.0.0",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        "magento/magento-composer-installer": "*"
    },
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "[Vendor]\\[ModuleName]\\": ""
        }
    }
}

Note the lowercase [vendor] and [modulename] in the composer file.

Honestly though, the “name” can be anything you like. Magento for example like to prefix it with “module-“. So, “magento/module-catalog” for example

Don’t forget to commit your handy work #vcs

  • magento2

Like this post? Share it :)


Related Posts

Back