Magento, Magento 2

Getting Started With Magento 2 Module Development

Getting Started With Magento 2 Module Development

Hello Everyone, Welcome to my blog. First of all, for those people who donโ€™t know anything about me please check out this link for more information. 

If you want to connect with me or want to discuss a specific topic, want to share your view or anything, please feel free to write a comment below or write me an email on my email id i.e ranade.ashish11@gmail.com. I will definitely try to address all your queries. 

So, first thing first ๐Ÿ™‚ This blog is completely for the technical audience who has a technical background or doing some development in Magento. In future blogs, I will share some deep understanding of module development and Magento customizations. Let’s get started… ๐Ÿ™‚

Here, we are going to create a simple Magento 2 module which will consist of basic files. Find the steps below that we are going to follow:

  1. Create a directory for our module.
  2. Create a registration file for our module.
  3. Create a module declaration file for our module.
  4. Register a module in Magento using a CLI command.


Create a directory for our Module

I am assuming that you have already installed the latest Magento 2 version on your local machine. To create a module, navigate to your Magento root directory and open the app folder. Here, we need to create a code(if it is not present) directory where all modules will reside. Create our Namespace <Ashish> and module name <FirstModule> directory inside the app/code directory. Folder structure should look like this [php] app/code/Ashish/FirstModule [/php]

FirstModule

Create a registration file for our module

Once we create a namespace and module name, now it’s time to create a registration file. The file location should be [php] Ashish/FirstModule/registartion.php [/php] Here I took Ashish as a Namespace and FirstModule as a module name. 

Registration file consists of \Magento\Framework\Component\ComponentRegistrar class [php] \Magento\Framework\Component\ComponentRegistrar [/php] To register our module in Magento we have to call its static function register and pass three parameters i.e. type, component name, and path.

Type: Type is a component type that we are going to create (Magento has different components like module, library, theme, language). I will cover all the types in my future blogs. So, for this blog, we are going to consider the type as โ€œmoduleโ€. You can check this list in the ComponentRegistrar class. The file location is [php] vendor/magento/framework/Component/ComponentRegistrar.php [/php]

Component: This is our Fully-qualified component name. In this case its Ashish_FirstModule. Magento uses this name to put in a specific type path i.e. this Ashish_FirstModule name will be pushed into the list of modules if it’s not already registered.

Path: we need to pass the PHP constant which returns the current directory of the file i.e __DIR__

Create a module declaration file

We can declare the component itself by adding a module.xml file in the [php] /etc [/php] folder of our module. The file location should be [php] app/code/Ashish/FirstModule/etc/module.xml [/php]

I am going to explain some of the attributes and tags used in the module.xml file.

Config: Unlike Magento 1, Magento 2 divides all the configuration into separate files like routes, modules, etc. So, all the configuration resides under the one configuration array which will be used for further modification or utilization, but that discussion is beyond the scope of this blog post. For now, we can consider that our module configuration will be pushed into the config array by Magneto.

Config tag has an attribute that defines the schema declaration file location, by using this XSD file Magento will validate your module XML file. 

Module: Module tag consists of two parameters, first one is the name which defines the name of your component, and the second one is setup_version. If you are using Declarative Schema to install or upgrade the database from your component, then you must add the setup_version parameter to the module tag. 

[php] [/php]

Register a Module using CLI

Magento has a command-line interface that performs both installation and configuration tasks. Magento is using a  Symfony library to register and execute your commands.

To enable our module in Magento we have to run the following command i.e. [php] php bin/magento module:enable Ashish_FirstModule [/php] To verify that our module is successfully registered, we can run the following command to check the status of our module.  [php] php bin/magento module:status Ashish_FirstModule [/php] You can also verify the status of your module from config.php i.e. [php] app/etc/config.php [/php]

You can also find this code on my Github account. Feel free to leave a comment below. Let me know what you think about this blog.

Goodbye for now and happy coding ๐Ÿ™‚

[post-views]

Tagged , ,

Leave a Reply

Your email address will not be published.