Initializing extension basics

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.

Getting ready

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/

How to do it…

Follow these steps to initialize a new extension:

  1. Create the module initialization file:

    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>
  2. Create the module registration file:

    registration.php

    <?php
    
    MagentoFrameworkComponentComponentRegistrar::register(
      MagentoFrameworkComponentComponentRegistrar::MODULE,
      'Genmato_Sample',
      __DIR__
    );
  3. Create the module 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\": ""
        }
      }
    }
  4. Enable the module with Magento. To do this, run the following command:
    bin/magento module:enable Genmato_Sample
    
  5. Run the upgrade command to register the module:
    bin/magento setup:upgrade
    

How it works…

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:

  • Module name: Genmato_Sample
  • Setup version: 0.1.3 is used for the setup/upgrade scripts creating database schemas
  • Sequence: Here we can define the modules that this extension is depending on

With 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/modules
  • LIBRARY: This is for extensions that are used as a library
  • THEME: This is for themes
  • LANGUAGE: This is for language packs

In 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/modules
    • magento2-theme: Themes
    • magento2-language: Language packages
  • require: 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.

There's more…

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:

  • Magento marketplace: Currently, installing Magento 2 through Composer is done from the Magento repository. In the future, paid and free modules/packages bought through the marketplace will be available through the same repository through your account. This will make installing and managing packages easy.
  • From version control repository: For private packages that you use in your project, it is possible to install them directly from your version control repository; for this, you need to add the following to the composer.json file located in your Magento 2 installation root:
    {
      "repositories": [
        {
          "type": "vcs",
          "url": "https://github.com/[github account]/[package]"
        }
      ]
    }
  • Packagist: For freely available packages, it is also possible to register them on Packagist, which is the default repository that is available in Composer to install packages. To add a Magento 2 extension to Packagist, you will need to store the extension in a public repository, which can be GitHub (http://www.github.com) or BitBucket (http://www.bitbucket.com).

    On Packagist, you can submit your package by specifying your extension repository URL:

    There's more…

Every package is now installable by running the following command in your Magento 2 installation root:

composer require <vendor-name>/<module-name>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.162.49