Getting started with Magento 2 module development

Posted February 6, 2016

I’ve been using Magento 2 for a few months now. I was lucky to be involved in the merchant_beta program and at the developer meetings in Berlin early last year. Yet lots has changed in the way we develop modules since I started working on the framework almost a year ago. One of the big changes is that now modules can be loaded from any location and require more configuration to setup.

Lets start by creating a new folder within app/code called Jcowie. Think of this as our top namespace.

Now within Jcowie create a new folder for HelloWorldModule This is going to be our actual working Module code. Now I’m still playing with ideas here as there is no technical reason why we need the Module suffix. Yet there are times where I will also have a HelloWorld namespace that is framework agnostic code that I will inject into the Magento 2 code.

Next up we need to add a composer.json manifest file. This is new to Magento 2 but not new to PHP. If you don’t know what composer is don’t worry there are many good guides on the internet and it WILL make you happy.

{
  "name": "Jcowie/HelloWorldModule",
  "description": "A Magento 2 module that creates a new page",
  "type": "magento2-module",
  "version": "1.0.0",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ],
  "require": {
    "php": "~5.5.0|~5.6.0|~7.0.0",
    "magento/framework": "~100.0"
  },
  "autoload": {
    "files": [ "registration.php" ],
    "psr-4": {
      "Jcowie\\HelloWorldModule\\": ""
    }
  }
}

Now there are some really important parts of this file to consider and make sure when you create your own modules you do the same. First key part is the type

"type": "magento2-module",

When Magento-Composer parses the files it looks for this type to ensure that both hard and soft dependencies can be read and processed. Ill look into this more in another post but for now just remember this is what makes a Magento 2 module different to a regular ol PHP module.

The final part of configuration that we need to add is the autoloading, this is what Magento 2 will parse when composer runs to ensure this module is added to the autoloader class and its functionality added into Magento 2:

"autoload": {
  "files": [ "registration.php" ],
  "psr-4": {
    "Jcowie\\HelloWorldModule\\": ""
  }
}

There is nothing too complex going on here. Using the composer standard we set a autoload node and define our namespace as Jcowie\\HelloWorldModule and where the src files should be loaded from. In this case this directory. We then tell composer that there is a special file registration.php that has a purpose and needs to be ran to complete the autoloading process.

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Jcowie_HelloWorldModule',
    __DIR__
);

Now this file almost never changes for all of the modules you will write, Just remember to update the Module name with yours.

Finally to make sure that Magento 2 knows about our module and when running php bin/magento setup:upgrade we know that it will load and process our module we need to update our projects composer.json file. Now this step depends on how you are building a module. This guide assumes that you are working on what I call a ‘Client Project’ where there is no shareable code so it will be added as part of the project itself. If you think or are working on a Module for the community then the workflow will be different.

"autoload": {
        "psr-4": {
            "Jcowie\\": "app/code/Jcowie/"
        },
    },

All we are doing here is to register the Jcowie namespace and where the files for this are located. Running composer dump-autoload will ensure all of the class maps etc are re generated and ready for the application. Finally if we run php bin/magento setup:upgrade we will see in the output that our module is now registered and just to confirm open up app/etc/config.php and you will see a reference to our new module.

What im working on next is how we can put our code anywhere we want. With the power we get from PSR autoloading we should be able to load code directly from src or any location we specify.

You may also find these related posts interesting: Patching Magento 2 vendor directory Technical Insights into Magento 2 Continously delivering Learn to love testing an agile journey into Behat, PHPSpec and Magento