What is necessary to create a very basic Magento 2 extension skeleton? Not that much. If you’re don’t have a patience to read the whole article – here is a link to download.
If you do – let’s start from basics.
1 |
php bin/magento deploy:mode:set developer |
Create module
In Magento 1 we have three code pool folders under app/code
directory: core
, community
and local
. It’s much simplified in Magento 2 – now all extensions located by their namespace in same directory: app/code
.
So to begin with module setup we need to create necessary files and folders. And here is a structure:
1 2 3 4 5 6 |
app/code |_ YourName |_ FooBar |_ etc |_ module.xml |_ registration.php |
Now, when we have module structure – let’s fill it with some code:
1. Add following to app/code/YourName/FooBar/registration.php
:
1 2 3 4 5 6 7 8 9 |
<?php /** * Basic Magento 2 extension skeleton */ \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'YourName_FooBar', __DIR__ ); |
2. And this to app/code/YourName/FooBar/etc/module.xml
:
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0"?> <!-- /** * Basic Magento 2 extension skeleton */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="YourName_FooBar" setup_version="1.0.0"> </module> </config> |
Yes, that’s it – bare minimum to register new Magento 2 extension.
Module setup
Now when we have all necessary module files and folders in place – let’s register and enable it.
Navigate to your magento root folder and run following commands in terminal:
1 2 |
php bin/magento module:enable --clear-static-content YourName_FooBar php bin/magento setup:upgrade |
This will enable your module, flush some cache and register it with the database.
Now go to Admin -> Stores -> Configuration -> Advanced -> Advanced
in admin panel. You should be able to see your module available there.
That’s all – in my next articles I will extend this basic skeleton and add a controllers, blocks, etc.