The way in which a new extension is built in Magento 2 is a bit different than it was in Magento 1. The major change is that all files are now included in the extension directory. This makes it easier to manage and remove.
The location of an extension is also different; there are no longer separate codepools as used in Magento 1 (core, community, and local). Depending on the way the extension is installed, the extension will be running from the vender
directory when installed through Composer. For project-specific extensions, it is also possible to place them in app/code
.
When developing an extension, it is advised to run Magento 2 in developer mode as this will give better error messages explaining what went wrong and make debugging a lot easier.
To display all PHP errors and activate developer mode, activate the display_errors
setting in app/bootstrap.php
and run the following command:
bin/magento deploy:mode:set developer
The code used in this chapter is based on naming the module Genmato_Sample
and all files will be placed in the following:
app/code/Genmato/Sample/
Follow these steps to initialize a new extension:
etc/module.xml:
<?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="Genmato_Sample" setup_version="0.1.3"> <sequence> <module name="Magento_Store"/> </sequence> </module> </config>
registration.php
<?php MagentoFrameworkComponentComponentRegistrar::register( MagentoFrameworkComponentComponentRegistrar::MODULE, 'Genmato_Sample', __DIR__ );
composer.json
file:composer.json
{ "name": "genmato/sample", "description": "Genmato Magento2 Sample extension", "keywords": ["magento2", "genmato", "m2sample"], "type": "magento2-module", "license": "OSL-3.0", "require": { "php": "~5.5.0|~5.6.0|~7.0.0" }, "autoload": { "files": [ "registration.php" ], "psr-4": { "Genmato\Sample\": "" } } }
bin/magento module:enable Genmato_Sample
bin/magento setup:upgrade
The module declaration in step 1 registers the module in Magento; here, the version number and dependencies are also specified to manipulate the load order. The following XML nodes are available:
Genmato_Sample
0.1.3
is used for the setup/upgrade scripts creating database schemasWith the use of Composer, modules can be placed in two locations currently. In order for the autoloader to know what file to load when a class is instantiated, the path needs to be registered. Through registration.php
from step 2, the (__DIR__
) location is stored for a specified package. The registration can be done for the following types of packages:
MODULE
: This is for extensions/modulesLIBRARY
: This is for extensions that are used as a libraryTHEME
: This is for themesLANGUAGE
: This is for language packsIn order to use Composer to install the module, it is required to add a composer.json
file to the module. The most important elements in this file are as follows:
Name
: The extension package name is in the format of <vendor name>/<extension name>;
it is important to only use lowercase letters. This name is also used to install the extension through Composer.Type
: This defines the package type; the possible options are as follows:magento2-module
: Extensions/modulesmagento2-theme
: Themesmagento2-language
: Language packagesrequire
: This defines the packages needed to be installed on the system for this package to work. During installation of the package, Composer will check the requirements and try to install the missing packages. When it's not possible to install them, the installation will fail.Autoload
: This element is to specify information that needs to be loaded through the Composer autoloader.In order to install packages in your project, they have to be available for Composer to install. There are a few options available to install packages from:
composer.json
file located in your Magento 2 installation root:{ "repositories": [ { "type": "vcs", "url": "https://github.com/[github account]/[package]" } ] }
On Packagist, you can submit your package by specifying your extension repository URL:
Every package is now installable by running the following command in your Magento 2 installation root:
composer require <vendor-name>/<module-name>
18.119.162.49