This guide contains information for developers who want to extend, or customize, any of the components in the Magento application. That includes building a new component from to extend Magento functionality.

magento 2.0

 

In Magento 2 , composer.json is introduced  to declare a dependency on third-party components or on the magento product as well as the class autoloader.

 As Magento Wiki Stated :  “Composer is a dependency manager for PHP. Magento 2 uses Composer to package components and product editions. Some third party components that the Magento system uses may be actually not present in the code base. Instead, they are listed as dependencies in the root composer.json  file.”

 Naming conventions

Naming convention is similar as magento 1.x had,

 <vendor_name>/<package_name>

 but the only different is, now every latter in name will be small later.

“All letters in the name must be lowercase. ”

 The package name is up to the vendor (as long as it is lowercase). If this name is meant to consist of multiple words, the Composer specification recommends separating them with dash. The convention for Magento package names is this:

magento/<type-prefix>-<suffix>[-<suffix>]...

Where:

type-prefix is a type of component in a Magento-specific domain.

suffix is anything that allows distinguishing/disambiguating the component within that type.

For eg:

magento/module-catalog-inventory

magento/module-checkout

magento/theme-frontend-blank

magento/theme-adminhtml-backend

magento/language-en_gb

magento/language-de_de

Magento-specific package types:

Each Magento component can be categorized into one of the types listed in the preceding table. If any component does not fit into a specific category, it can be generalized to magento-component.

Having an identifier type for each component allows the system to marshal the directories and files of each component to the correct locations, based on the Magento 2 directory structure. ”

 Building your component:

Prerequisites

The following details the component building process:

Module Structure:

As we know Magento2 have different file structure as compare to magento 1.x. so here is the following recommended dir structure:

Subdirectory of app :

  • For modules,  app/code
  • For themes,  app/design/frontend or app/design/adminhtml 
  • For language packages,  app/i18n

vendor:

You get this directory structure if you used the composer create-project command to get a Magento 2 metapackage (which downloads the CE or EE code), or if you extracted a compressed.

 Before proceeding there is some required files for module development.

The following are required for all components:

  • registration.php: Among other things, specifies the directory in which the component is installed; by default, components install in the

<Magento root dir>/vendor directory. And for your custom module it will be reside in you <namespace>/<package>/

  • composer.json: Specifies component dependencies.

Module File Structure Recommended by Magento2:  app/code/<Namespace>/

 

 

 Typical directories

Typical module directories are:

  • Block: contains PHP view classes as part of MVC vertical implementation of module logic.
  • Controller: contains PHP controller classes as part of MVC vertical implementation of module logic.
  • etc: contains configuration files; in particular, module.xml, which is required.
  • Model: contains PHP model classes as part of MVC vertical implementation of module logic.
  • Setup: contains classes for module database structure and data setup which are invoked when installing or upgrading.

Additional directories

Additionally, there are directories for configuration and other ancillary functions for items like plug-ins, internationalization, and layout files.

  • Api: contains any PHP classes exposed to the API.
  • i18n: contains localization files.
  • Plugin: contains any needed plug-ins.
  • view: contains view files, including static view files, design templates, email templates, and layout files.

 Controllers:

 As it has change the way it has coded earlier. As Earlier we had <Controller_name>Controller.php which contains number of actions to be executed. But Now We would have following Structure for controller:

 like: if url is like: http://localhost/magento2/test/index/hello

 then:

test -> is module name

index-> is controller name

hello-> is action name

 so for now inside Test module / Controller/ we would have

Index/Hello.php file.

Which will extends \Magento\Framework\App\Action\Action class  which has a absctract method execute so all the coding logic will be written inside this method.