{"id":21864,"date":"2023-10-26T08:01:19","date_gmt":"2023-10-26T08:01:19","guid":{"rendered":"https:\/\/ecommerce.folio3.com\/blog\/?p=21864"},"modified":"2023-11-14T10:22:53","modified_gmt":"2023-11-14T10:22:53","slug":"create-a-magento-2-module","status":"publish","type":"post","link":"https:\/\/ecommerce.folio3.com\/blog\/create-a-magento-2-module\/","title":{"rendered":"How to Create a Magento 2 Module (A Developer\u2019s Guide)"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Creating a Magento 2 module is a fundamental process in extending the functionality of your e-commerce store. By establishing a structured module, crafting controllers to handle user interactions, and applying layout XML, blocks, and helpers for content presentation and utility functions, you can enhance your store&#8217;s features and design with flexibility and customization in mind. This modular approach empowers you to create tailored solutions that align with your business needs and customer expectations, making your Magento store a more powerful and engaging platform.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">What is Module in Magento 2?<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">In Magento 2, a module is a self-contained unit of functionality that allows you to extend and customize the platform&#8217;s features and behavior. Modules encapsulate code, templates, configurations, and other resources related to a specific task or feature. They follow a structured directory hierarchy and can include controllers for handling user requests, layout XML files for page customization, blocks for managing content, and helpers for utility functions. Modules are integral to Magento&#8217;s modular architecture, enabling developers to add, remove, or modify functionality without affecting the core system. This modularity and flexibility make Magento 2 highly adaptable and customizable for e-commerce businesses.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Module Structure (Files and Folders)<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Creating a module in Magento involves establishing a structured directory hierarchy and including essential configuration files. Below is the recommended directory structure for a Magento 2 module:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-21865 size-full\" src=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Module-Structure.png\" alt=\"Module Structure\" width=\"640\" height=\"641\" srcset=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Module-Structure.png 640w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Module-Structure-300x300.png 300w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Module-Structure-150x150.png 150w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Module-Structure-370x370.png 370w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Let&#8217;s jump into the technical details about the explanation of each step in creating a Magento 2 module.\u00a0<\/span><\/p>\n<p><b>Note:<\/b><span style=\"font-weight: 400;\">\u00a0 We will just create the required files and folders that need to set up a basic Magento 2 module without jumping into more technical details regarding the above-mentioned files and folders.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">1. Create the Module Directory:<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Start by creating a directory for your module in the <\/span><strong>app\/code <\/strong><span style=\"font-weight: 400;\">directory of your Magento installation. The directory structure should be <\/span><strong>app\/code\/&lt;Vendor&gt;\/&lt;Module&gt;<\/strong><span style=\"font-weight: 400;\">, where <\/span><strong>&lt;Vendor&gt;<\/strong><span style=\"font-weight: 400;\"> is your company or personal namespace, and <\/span><strong>&lt;Module&gt;<\/strong><span style=\"font-weight: 400;\"> is the name of your module. This directory will contain all your module&#8217;s code and resources.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">2. Define registration.php:<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Inside your module directory, create a registration.php file. This file registers your module with Magento. It should contain PHP code like this:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-21866 size-full\" src=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Define-registration.png\" alt=\"Define registration\" width=\"625\" height=\"131\" srcset=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Define-registration.png 625w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Define-registration-300x63.png 300w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"font-weight: 400;\">3. Set up module.xml:<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Inside your module directory, create an etc directory and a module.xml file. This XML file defines your module and its version:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-21867 size-full\" src=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Set-up-module.png\" alt=\"Set up module\" width=\"746\" height=\"133\" srcset=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Set-up-module.png 746w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Set-up-module-300x53.png 300w\" sizes=\"(max-width: 746px) 100vw, 746px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"font-weight: 400;\">4. Design a Controller:<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Create a controller for your module to manage user requests. Inside your module directory, create a <\/span><strong>Controller<\/strong><span style=\"font-weight: 400;\"> directory and a subdirectory corresponding to your controller&#8217;s route. For example, <\/span><strong>Controller\/Index\/Index.php<\/strong><span style=\"font-weight: 400;\"> could be a simple controller:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-21868 size-full\" src=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Design-a-Controller.png\" alt=\"Design a Controller\" width=\"724\" height=\"346\" srcset=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Design-a-Controller.png 724w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Design-a-Controller-300x143.png 300w\" sizes=\"(max-width: 724px) 100vw, 724px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"font-weight: 400;\">5. Craft routes.xml:<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">In your module&#8217;s etc directory, create a frontend\/routes.xml file to define your custom route and link it to the controller. This XML file is essential for routing user requests to your controller:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-21869 size-full\" src=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Craft-routes.png\" alt=\"Craft routes\" width=\"724\" height=\"200\" srcset=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Craft-routes.png 724w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Craft-routes-300x83.png 300w\" sizes=\"(max-width: 724px) 100vw, 724px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Here is the detail about the xml sections:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>&lt;route id=&#8221;routename&#8221;&gt;:<\/b><span style=\"font-weight: 400;\"> This is the custom route ID that you define. It&#8217;s used to uniquely identify your route and can be anything you choose.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>&lt;frontName&gt;frontname&lt;\/frontName&gt;:<\/b><span style=\"font-weight: 400;\"> This is the part of the URL that will be associated with your custom route. For example, if the frontname is &#8220;myvendor,&#8221; you can access your controller actions via URLs like <a href=\"http:\/\/yourstore.com\/myvender\/controller\/index\" target=\"_blank\" rel=\"nofollow noopener\">http:\/\/yourstore.com\/myvender\/controller\/index<\/a>.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>&lt;module name=&#8221;MyVendor_MyModule&#8221;&gt;:<\/b><span style=\"font-weight: 400;\"> This specifies the module that handles the requests for this route. Replace &#8220;MyVendor_MyModule&#8221; with your module&#8217;s actual name.<\/span><\/li>\n<\/ul>\n<h3><span style=\"font-weight: 400;\">6. Add Layout XML:<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">In the view\/frontend\/layout directory of your module, you can create XML files to define the layout updates for your controller&#8217;s action. This is where you structure the page content and specify which blocks to include. For example, create\u00a0<\/span><\/p>\n<p><b><i>\u2018<\/i><\/b><b><i>Myvendor_mymodule_index_index.xml\u2019<\/i><\/b><span style=\"font-weight: 400;\">:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-21870 size-full\" src=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Add-Layout-XML.png\" alt=\"Add Layout XML\" width=\"870\" height=\"223\" srcset=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Add-Layout-XML.png 870w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Add-Layout-XML-300x77.png 300w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Add-Layout-XML-768x197.png 768w\" sizes=\"(max-width: 870px) 100vw, 870px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"font-weight: 400;\">7. Develop a Block:<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Inside your module, create a Block directory and a PHP block class. This block class will be referenced in the layout XML. Here&#8217;s an example:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-21871 size-full\" src=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Develop-a-Block.png\" alt=\"Develop a Block\" width=\"453\" height=\"221\" srcset=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Develop-a-Block.png 453w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Develop-a-Block-300x146.png 300w\" sizes=\"(max-width: 453px) 100vw, 453px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"font-weight: 400;\">8. Create a Template:<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">In the <\/span><span style=\"font-weight: 400;\">view\/frontend\/templates<\/span><span style=\"font-weight: 400;\"> directory of your module, create a template file that corresponds to the template referenced in your layout XML. For example, create\u00a0<\/span><\/p>\n<p><b><i>\u2018my_template.phtml\u2019<\/i><\/b><span style=\"font-weight: 400;\">:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-21872 size-full\" src=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Create-a-Template.png\" alt=\"Create a Template\" width=\"464\" height=\"49\" srcset=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Create-a-Template.png 464w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Create-a-Template-300x32.png 300w\" sizes=\"(max-width: 464px) 100vw, 464px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"font-weight: 400;\">9. Implement a Helper:<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Inside your module, create a Helper directory and a PHP helper class. This helper class can include utility functions to assist with various tasks within your module:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-21873 size-full\" src=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Implement-a-Helper.png\" alt=\"Implement a Helper\" width=\"617\" height=\"180\" srcset=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Implement-a-Helper.png 617w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Implement-a-Helper-300x88.png 300w\" sizes=\"(max-width: 617px) 100vw, 617px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"font-weight: 400;\">10. Enable and Test the Module:<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">In your Magento root directory, enable your module using the following command:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-21874 size-full\" src=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Enable-and-Test-the-Module.png\" alt=\"Enable and Test the Module\" width=\"403\" height=\"37\" srcset=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Enable-and-Test-the-Module.png 403w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Enable-and-Test-the-Module-300x28.png 300w\" sizes=\"(max-width: 403px) 100vw, 403px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">After enabling your module, you should clear the cache to see your changes:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-21875 size-full\" src=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Test-the-Module.png\" alt=\"Test the Module\" width=\"389\" height=\"31\" srcset=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Test-the-Module.png 389w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Test-the-Module-300x24.png 300w\" sizes=\"(max-width: 389px) 100vw, 389px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">After this, check the module output by visiting the given url:<\/span><\/p>\n<p><a href=\"http:\/\/yourstore.com\/myvender\/controller\/index\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">http:\/\/yourstore.com\/myvender\/controller\/index<\/span><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-21876 size-full\" src=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Module-Output.png\" alt=\"Module Output\" width=\"1429\" height=\"280\" srcset=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Module-Output.png 1429w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Module-Output-300x59.png 300w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Module-Output-1024x201.png 1024w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Module-Output-768x150.png 768w\" sizes=\"(max-width: 1429px) 100vw, 1429px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"font-weight: 400;\">Conclusion:<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">This comprehensive article has illuminated the essential steps for crafting a module in Magento 2, equipping you with the fundamental knowledge required to embark on the development phase. In the subsequent articles, we will explore the intricate art of building different sections like blocks, helpers and crafting layout XML in Magento 2. These advanced insights will further elevate your proficiency in module development, enabling you to tailor your e-commerce store with finesse and precision.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating a Magento 2 module is a fundamental process in extending the functionality of your e-commerce store. By establishing a structured module, crafting controllers to handle user interactions, and applying layout XML, blocks, and helpers for content presentation and utility functions, you can enhance your store&#8217;s features and design with flexibility and customization in mind.<\/p>\n","protected":false},"author":37,"featured_media":21877,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[27],"tags":[148,147],"class_list":{"0":"post-21864","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-magento","8":"tag-magento-2-module","9":"tag-module-in-magento-2"},"acf":[],"featured_image_data":{"src":"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2023\/11\/Module-scaled.jpg","alt":"Module","caption":""},"_links":{"self":[{"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/posts\/21864"}],"collection":[{"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/comments?post=21864"}],"version-history":[{"count":0,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/posts\/21864\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/media\/21877"}],"wp:attachment":[{"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/media?parent=21864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/categories?post=21864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/tags?post=21864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}