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.
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 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
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. ”
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 :
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:
<Magento root dir>/vendor directory. And for your custom module it will be reside in you <namespace>/<package>/
Module File Structure Recommended by Magento2: app/code/<Namespace>/
Typical module directories are:
Additionally, there are directories for configuration and other ancillary functions for items like plug-ins, internationalization, 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.