magento2 console command module

Hey there! With this post I will start a series of short tutorials called Quick Ones. On the begining I will focus on what I do everyday – Magento 2, so let us start with a magento2 sample module.

In this tutorial we will make a base module – it will do nothing special so treat it as skeleton for creating all other modules.

NOTE: you can find the source code of the module here -> source

Module Structure

Magento custom modules should either land in app/code directory or in vendor (if you use composer for adding modules to your project). As we are creating a custom module we will create it in the app/code.

Each module has to have namespace folder and inside it a module folder. The first is usualy a company name, for all my tutorials I will use Ruchlewicz as the name of that folder. The second one should tell us something about the module. So if we are creating a blog it would be Blog, if we are creating a module that will modify a shipment we can name the folder Shipment. In our case it will be Sample

Magento requires only two files for module. First is etc/module.xml and the second is registration.php

etc/module.xml

In our module folder we need to create an etc folder and inside it a module.xml file. Etc folder will contain all the module xml files, we will get to know about other files in next tutorials.

Ruchlewicz/Sample/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="Ruchlewicz_Sample" setup_version="1.0.0">
    </module>
</config>

This file is telling magento what is the current version of the module.

registration.php

Next we need to create the registration.php file in the module folder.

Ruchlewicz/Sample/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Ruchlewicz_Sample',
    __DIR__
);

As the file name says it registers the module.

TIP: When you will create new modules in the future and you will not use some IDE plugin for creating modules, you will usually copy those two files and just change the module name and version inside – I do it like this 😛 So you do not have to remember the classes that are used in the registration.php file.

Checking the module

Our module is done, so it would be good to check if it works. As it does not do anything, the only way to check if magento sees our module is to run a bin/magento module:status console command.

Console Commands

I will create another post about console commands in magento2, but for now you can just follow the instructions.
To check what modules are installed in magento open a terminal and

cd your_magento_dir
php bin/magento module:status

the second command should list all of installed modules, yours should be disabled for now so it will be at the end of the list.

That is it, you have created your first magento module 😉 Do not worry, in next posts we will make it do something 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.