<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Richard</title>
    <description>The latest articles on Forem by Richard (@nonimpressed).</description>
    <link>https://forem.com/nonimpressed</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F456175%2F22a38061-662c-4615-909c-bdaeff4a7dba.png</url>
      <title>Forem: Richard</title>
      <link>https://forem.com/nonimpressed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nonimpressed"/>
    <language>en</language>
    <item>
      <title>Module Development in Magento 2 (Adobe Commerce): Registering a Module</title>
      <dc:creator>Richard</dc:creator>
      <pubDate>Sun, 21 Aug 2022 20:54:14 +0000</pubDate>
      <link>https://forem.com/nonimpressed/module-development-in-magento-2-adobe-commerce-registering-a-module-3c7o</link>
      <guid>https://forem.com/nonimpressed/module-development-in-magento-2-adobe-commerce-registering-a-module-3c7o</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;I've been getting involved in Magento 2 (Adobe Commerce) module development for a project at work.&lt;/p&gt;

&lt;p&gt;I'm not particularly versed in Magento 2 but I've been developing for a few years now and I've learnt that I can't resist picking up something new and the inevitable challenge that ensues.&lt;/p&gt;

&lt;p&gt;While working on this project I've found that the &lt;a href="https://m.academy/courses/"&gt;best Magento 2 resources&lt;/a&gt; are behind paywalls and the &lt;a href="https://experienceleague.adobe.com/docs/commerce.html"&gt;offical documentation&lt;/a&gt;, while extensive, is not easy to digest.&lt;/p&gt;

&lt;p&gt;The aim of this series of posts is to cover a few aspects of Module development in the Magento 2 ecosystem and how to use a Module to make customisations to a store.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why
&lt;/h2&gt;

&lt;p&gt;I find that I don't feel like I fully understand something unless I can explain it to someone else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Assumptions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You have an install of Magento 2 that you can develop with (if you don't then &lt;a href="https://github.com/markshust/docker-magento"&gt;this repo&lt;/a&gt; with docker will work wonders.&lt;/li&gt;
&lt;li&gt;You've heard of and know some basics of XML, PHP, HTML etc&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Where do I develop Modules
&lt;/h2&gt;

&lt;p&gt;My first mistake was assuming that I should develop my module immediately as a vendor file. Magento module development should always be made in the &lt;strong&gt;app/code&lt;/strong&gt; folder. This allows you to easily tweak and edit the module without having to run too many special commands.&lt;/p&gt;

&lt;p&gt;You'll eventually be able to package up the module for distrubution, but development is best in this location.&lt;/p&gt;

&lt;h2&gt;
  
  
  Useful Commands
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;bin/magento setup:upgrade&lt;/strong&gt; : Registers and reinitialises modules&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;bin/magento cache:flush&lt;/strong&gt; : Clears the various caches to force changes&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  File Structure
&lt;/h2&gt;

&lt;p&gt;Inside the &lt;strong&gt;app/code&lt;/strong&gt; folder you'll need to place your module within a namespace folder. Typically this will be your company name. This name needs to be as unique as possible to avoid conflict with every other company name making modules for Magento 2. A company name is usually sufficient.&lt;/p&gt;

&lt;p&gt;Within your namespace folder you'll have your module folder. And within the module folder you'll have a few different folders and files that handle various things. You won't need all of these, depending on what you want to achieve, but it's good to know what's possible.&lt;/p&gt;

&lt;p&gt;If you'd rather read the official docs, &lt;a href="https://developer.adobe.com/commerce/php/development/build/component-file-structure/"&gt;here is the link!&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Companyname [namespace]
-- Modulename [module name]
---- Api 
---- Block [Content block creation]
---- Controller
---- Console
---- etc
------ adminhtml [config for modifying/adding functionality]
-------- di.xml
-------- events.xml
-------- menu.xml
-------- routes.xml
-------- system.xml
------ frontend [config for modifying/adding functionality]
-------- di.xml
-------- events.xml
-------- page_types.xml
-------- routes.xml
------ webapi_rest
-------- di.xml
------ webapi_soap
-------- di.xml
------ acl.xml
------ config.xml
------ di.xml
------ eav_attributes.xml
------ module.xml
------ webapi.xml
---- Helper [For reuseable code across the module]
---- i18n
---- Model
---- Plugin [For hooking into functions]
---- view [holds templates]
---- composer.json
---- registration.php [registers the module]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Registering the module
&lt;/h2&gt;

&lt;p&gt;You won't need all of the above files to just get the module registered. All you need for this is the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Companyname [namespace]
-- Modulename [module name]
---- etc
------ module.xml
---- registration.php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The contents of the &lt;strong&gt;module.xml&lt;/strong&gt; file will need to be something like the below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?xml version="1.0"?&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;config&lt;/span&gt; &lt;span class="na"&gt;xmlns:xsi=&lt;/span&gt;&lt;span class="s"&gt;"http://www.w3.org/2001/XMLSchema-instance"&lt;/span&gt; &lt;span class="na"&gt;xsi:noNamespaceSchemaLocation=&lt;/span&gt;&lt;span class="s"&gt;"urn:magento:framework:Module/etc/module.xsd"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;module&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"Companyname_Modulename"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/config&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the contents of the &lt;strong&gt;registration.php&lt;/strong&gt; file will need to match it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Magento\Framework\Component\ComponentRegistrar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;ComponentRegistrar&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;ComponentRegistrar&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;MODULE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'Companynamespace_Modulename'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;__DIR__&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you have this in place you can run this command using the Magento CLI&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bin/magento setup:upgrade
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your module will now be registered and active. However it doesn't really do anything yet, it's just working.&lt;/p&gt;

&lt;h2&gt;
  
  
  What next?
&lt;/h2&gt;

&lt;p&gt;You'll now be able to start adding files and folders and functionality to the module to make it suit your needs. Within this module you can pretty much change anything and everything in Magento 2.&lt;/p&gt;

&lt;p&gt;In my next article I'll look at adding additional functionality to the settings files and adding additional items to the admin menu.&lt;/p&gt;

&lt;p&gt;If you do venture ahead on your own, remember that you will need to clear the cache between updates, and occasionally recompile.&lt;/p&gt;

&lt;p&gt;Thanks for reading my learning notes!&lt;/p&gt;

</description>
      <category>magento</category>
      <category>todayilearned</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
