In this tutorial, we will build a basic extension for Magento 2. This extension will not work on its own. However it is the first step in the construction of any extension for Magento 2 of any type, and it is a mandatory step as well.
Before we start, there's an important step that we need to do:
The easiest way to turn off the cache is to go to your Magento admin panel → System → Cache Management → At Mass Action choose "Select All" → At Actions choose "Disable" instead of "Refresh" → Now click on "Submit".
At this point all your caches must show "DISABLED" status like in my screenshot:
Now that we have disabled the cache, we can continue by creating the folder structure and the mandatory files that Magento requires in order to recognize your extension.
1. Create the following structure
You must note that if you are working with a fresh install, you will also need to create the folder app/code/ because this is not created by default.
In this example I will create an extension for testimonials, so for <Vendor> I will use DEVLINE and Testimonial for <Extension>, so I will have the following folder structure:
2. Now let's register our module by creating a registration.php in the folder app/code/DEVLINE/Testimonial/ and adding the following content:
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'DEVLINE_Testimonial', __DIR__ );
3. The last step is to name and declare our module by creating a module.xml in the folder app/code/DEVLINE/Testimonial/etc and adding the following content:
<?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="DEVLINE_Testimonial" setup_version="1.0.0"/> </config>
Both attributes of the <module> tag are required, even if you do not have a database schema.
At this point, you have all the necessary files for a Magento 2 extension. All you need to do is to run the next command in your terminal:
php bin/magento setup:upgrade
If this ran successfully, you would be able to see your extension in your admin panel already.
To check this you have to go to Magento admin panel → Stores → Configuration → Advanced → Advanced and search for your extension in the list. You need to see yours like in my screenshot:
You have successfully created a basic extension for Magento 2. This extension does nothing, but at the same time, it is necessary for creating admin grid, blocks, controllers and models.
P.S.: Do not forget to enable your cache :)