Install prerequisites and create the module skeleton

Prerequisites

Before getting started, be sure to have your prerequisites checked off. This includes enabling this project in PhpStorm as a Magento project, enabling PHPMD, PHPCS, and PHPCSF, setting up Magento coding standard validations, setting up the EA Sniffs module and so on.

I am also sure to set the current active version of PHP in PhpStorm to 8.1. This helps support the widest range of currently-up-to-date Magento sites out there. Also, ensure you have Xdebug enabled, as we’ll be using it to walk through how a Magento route is processed.

If you need help setting these up, check out my M2 Docker Development environment course, which goes over how to set this up, plus a lot more.

We’ll make use of both Beeline and Github Copilot throughout this course, as they just save a ton of time typing. I like to use both tools in my normal development workflow. Beeline is great at generating full files or blocks of code according to Magento coding guidelines & standards, and Copilot is great for smaller blocks of code while I’m working within functions.

🟢 Enable before starting:

  • Magento and PHP EA modules
  • Magento project support
  • Generate URN mappings
  • Proper PHP version (8.1) and PHP interpreter from Docker
  • PHPCS, PHPCSF, and PHPMD (contains Magento Coding Standard checks)

Create the module skeleton

Let’s get right into some code. First, we’ll create the skeleton for our custom module.

Since we are building a product comparison module, let’s create our module with the vendor name Macademy and the module name ProductCompare. It will include the standard registration.php, module.xml, and composer.json files, so let’s go ahead and populate these with Beeline.

registration.php

<?php

declare(strict_types=1);

use Magento\\Framework\\Component\\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Macademy_ProductCompare',
    __DIR__,
);

etc/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="Macademy_ProductCompare">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

composer.json

{
    "name": "macademy/magento2-module-productcompare",
    "description": "The ProductCompare module compares products by SKUs.",
    "require": {
        "php": "^8.1",
        "magento/framework": "^103"
    },
    "require-dev": {
        "roave/security-advisories": "dev-latest"
    },
    "type": "magento2-module",
    "version": "1.0.0",
    "license": [
        "MIT"
    ],
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "Macademy\\\\ProductCompare\\\\": ""
        }
    }
}

ℹ️ Beeline keywords: m2registration, m2module, m2composerModule

You may not be used to creating composer.json files for your modules, but they are important, because this file lets you define hard dependencies, which we will find out more about later on in the course.

Then, let’s go ahead and activate and enable the module with:

bin/magento module:enable Macademy_ProductCompare
bin/magento setup:upgrade


Complete and Continue  
Extra lesson content locked
Enroll to access all lessons, source code & comments.
Enroll now to Unlock