How to Build a New Magento 2 Extension? Let's Try This!

build magento 2 extension

Let’s look at short overview regarding Magento 2 history. Announced in 2010 this platform was considered as a great step forward by developers from many countries, including Germany, the UK,the USA and others. It was promised to perform many competitive advantages such as faster deployment, better scalability, easier customization and more flexible integration of extensions. A lot of ecommerce shop owners were ready to migrate to Magento 2 as soon as possible. And finally they had such opportunity after final developer release. It occurred in 2015, but before developers were able to study 2014 Magento 2 beta version and offer their opinions.

Many developers want to share their knowledge, therefore the Internet is filled with a great number of articles and tutorials on Magento 2. So, to increase your knowledge and skills, we want to show you how to create a new Magento 2 Extension. We are going to cover some development practices and techniques, which are certainly useful for Magento developers. Follow to the next page https://github.com/maksek/m2experience to get some essential Modules. Surely, this extension is built around them.

3 main stages of creating Magento 2 Extension

Git Clone

First of all, you have to create the structure and save it to the appropriate place. Magento 2 deveIopers tried their best and made it easy to do anything with the module repository. The same goes for the Magento 2 repository. Simply copy the Magento 2 repo into the local environment. Below, you can find an example showing how to do it:

mkdir maksek-magento2

cd maksek-magento2

git clone https://github.com/magento/magento2 .

Having created the extension repository, you have to make a copy of it. For modules you have to write own vendor name. It can be any name, like “Maxyek”, for example. To connect two repositories you need to make a git submodule.  Look at the example:

cd magento2/app/code

git submodule add https://github.com/maksek/m2experience.git Maxyek

cd Maxyek

git pull

That's it, a local Magento 2 repository exists. You also need to update your code regularly. This is where remote will help you, updating the code on each issue of Magento2. From now on, it is possible to save Magento Modules by using github, since app/code has a new code pool. As we explained earlier, you will be able to use pull and push commands to make alterations or amendments into the extension repository thanks to the fact that a submodule serves as a connection. More than that, Magento 2 was bettered further on October, 2015, so now modules are supported in the vendor directory. That is why we would recommend you to install Magento 2 by using the composer. Understandably, you should save new modules in this directory.Having created the extension repository, you have to make a copy of it. For modules you have to write own vendor name. It can be any name, like “Maxyek”, for example. To connect two repositories you need to make a git submodule.  Look at the example:

ExtendedConfig

Firstly, you should create ExtendedConfig module. A new section as well as some basic settings will be added to System Configuration. Later, we will indicate them. Then, you have to create the module. But to introduce it for Magento 2 you need to add define basic config. So. how you can do that? It is easy, you just need to add etc/module.xml config, registration.php and composer.json if you are going to use composer packages.

magento 2 extended config

module.xml that includes module name, dependencies and versions is the main config file for every module:

<?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="Maxyek_ExtendedConfig" setup_version="0.0.1"> 
   <sequence> 
       <module name="Magento_Config"/> 
   </sequence> 
</module> 
</config>

UPD: registration.php 

Every Magento 2 configuration has XSD schema. It makes possible to validate all configurations while editing in IDE. Use it to save own time. We declared just one dependency in config – Magento_Config. You  have to load it before the module, and of course, module name and initial version must be announced.

Since October 2015, every Magento 2 component has to contain registration.php file in the root directory. Magento 2 component might be module, library, language or  theme. Each component type is defined  in Magento\Framework\Component\ComponentRegistrar.

// Module registration
ComponentRegistrar::register(ComponentRegistrar::MODULE, '_', __DIR__);
// Theme 
ComponentRegistrar::register(ComponentRegistrar::THEME, '//', __DIR__);
// Language 
ComponentRegistrar::register(ComponentRegistrar::LANGUAGE, '_', __DIR__);
// Libraries
ComponentRegistrar::register(ComponentRegistrar::LIBRARY, '', __DIR__);

egistration.php should have such content:

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
   \Magento\Framework\Component\ComponentRegistrar::MODULE,
   'Maxyek_ExtendedConfig',
   __DIR__
);

In case of using composer packages, each component’s composer.json has to invoke the file in the autoload section, for instance:

{
 "name": "maxyek/module-extendedconfig",
 "description": "N/A",
 "require": {
   "php": "~5.5.0|~5.6.0|~7.0.0",
   "magento/module-config": "1.0.0-beta",
   "magento/framework": "1.0.0-beta"
 },
 "type": "magento2-module",
 "version": "0.0.1",
 "autoload": {
   "files": [ "registration.php" ],
   "psr-4": {
     "Maxyek\\ExtendedConfig\\": ""
   }
 }
}

Now, Magento 2 instance should be installed. The next tutorial will help you with it – http://devdocs.magento.com/guides/v2.0/install-gde/bk-install-guide.html.As Magento 2 uses PSR-4 standard, you can place your module everywhere together with correct registration.php settings. It will inform Magento 2 about a new module with autoloader parameters.

During installation, on “Step 4: Customize Your Store”, please look at “Advanced Modules Configurations” section, and you will see the new module Maxyek_ExtendedConfig there. It is new Magento 2 module. If you have pre-installed Magento 2 instance, then you need to carry out setup update:

php bin/magento setup:upgrade

So, now you have both new module and running instance of Magento 2. We hope that you have met this challenge successfully. But if you have any questions, just contact our Sam Ecommerce Magento Development team, and they will help you.

Also, on our blog, you can find more articles about Magento 2, for example:

The original article on building Magento 2 extension is found here: https://goo.gl/BTJbtK 

Comments (2):
Posted on Wednesday, September 14, 2016 by :
Thanks for Amazing Blog. I would like to suggest other some of Magento 2 extension with free extensions: https://goo.gl/k6DAIb
Posted on Wednesday, September 14, 2016 by :
Your website has to be the eloitrencc Swiss army knife for this topic.
Leave a comment:
*Your comment will be published after approval by site administrator.