|
| 1 | +# Magento module overview |
| 2 | +>A module is a logical group – that is, a directory containing blocks, controllers, helpers, models – that are related to a specific business feature. In keeping with Magento’s commitment to optimal modularity, a module encapsulates one feature and has minimal dependencies on other modules. - [Magento DevDocs - Module overview](http://devdocs.magento.com/guides/v2.2/architecture/archi_perspectives/components/modules/mod_intro.html) |
| 3 | +> |
| 4 | +>Modules and themes are the units of customization in Magento. Modules provide business features, with supporting logic, while themes strongly influence user experience and storefront appearance. Both components have a life cycle that allows them to be installed, deleted, and disabled. From the perspective of both merchants and extension developers, modules are the central unit of Magento organization. - [Magento DevDocs - Module overview](http://devdocs.magento.com/guides/v2.2/architecture/archi_perspectives/components/modules/mod_intro.html) |
| 5 | +> |
| 6 | +>The Magento Framework provides a set of core logic: PHP code, libraries, and the basic functions that are inherited by the modules and other components. - [Magento DevDocs - Module overview](http://devdocs.magento.com/guides/v2.2/architecture/archi_perspectives/components/modules/mod_intro.html) |
| 7 | +
|
| 8 | + |
| 9 | +Modules can be installed with Composer allowing their version management. All modules installed in such way are placed in the `vendor/` folder and have next base structure `vendor/<vendor>/<type>-<module-name>`. In this case `<type>` can be: |
| 10 | +1. `module` - Magento module |
| 11 | +1. `theme` - admin or frontend themes |
| 12 | +1. `language` - language packs |
| 13 | + |
| 14 | +In a case when you have a very specific functionality or customisation which is related to a specific project and there is no need to share it with other projects it should be created in the `app/code/<vendor>/<type>-<module-name>` directory and the required directories within it. |
| 15 | + |
| 16 | +### Module registration |
| 17 | + |
| 18 | +>Magento components, including modules, themes, and language packages, must be registered in the Magento system through the Magento ComponentRegistrar class. - [Magento DevDocs - Register your component](http://devdocs.magento.com/guides/v2.2/extension-dev-guide/build/component-registration.html) |
| 19 | +
|
| 20 | +>Each component must have a file called registration.php in its root directory. For example, here is the registration.php file for Magento’s AdminNotification module. Depending on the type of component, registration is performed through registration.php by adding to it as follows: - [Magento DevDocs - Register your component](http://devdocs.magento.com/guides/v2.2/extension-dev-guide/build/component-registration.html) |
| 21 | +
|
| 22 | +- How to register modules - registration.php [1] |
| 23 | +```php |
| 24 | +<?php |
| 25 | + |
| 26 | +use \Magento\Framework\Component\ComponentRegistrar; |
| 27 | + |
| 28 | +ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_AdminNotification', __DIR__); |
| 29 | +``` |
| 30 | + |
| 31 | + |
| 32 | +- how to register language packages - registration.php [2] |
| 33 | +```php |
| 34 | +<?php |
| 35 | + |
| 36 | +use \Magento\Framework\Component\ComponentRegistrar; |
| 37 | + |
| 38 | + ComponentRegistrar::register(ComponentRegistrar::LANGUAGE, '<VendorName>_<packageName>', __DIR__); |
| 39 | +``` |
| 40 | + |
| 41 | +- how to register themes - registration.php [3] |
| 42 | +```php |
| 43 | +<?php |
| 44 | + |
| 45 | +use \Magento\Framework\Component\ComponentRegistrar; |
| 46 | + |
| 47 | + ComponentRegistrar::register(ComponentRegistrar::THEME, '<area>/<vendor>/<theme name>', __DIR__); |
| 48 | +``` |
| 49 | + |
| 50 | + |
| 51 | +- composer.json autoload/files[] = "registration.php" [4] |
| 52 | +```json |
| 53 | + { |
| 54 | +"name": "Acme-vendor/bar-component", |
| 55 | +"autoload": { |
| 56 | + "psr-4": { "AcmeVendor\\BarComponent\\": "" }, |
| 57 | + "files": [ "registration.php" ] |
| 58 | +} } |
| 59 | +``` |
| 60 | + |
| 61 | +- at what stage - when including vendor/autoload.php [5] |
| 62 | +```php |
| 63 | + <?php |
| 64 | +/** |
| 65 | + * Copyright © Magento, Inc. All rights reserved. |
| 66 | + * See COPYING.txt for license details. |
| 67 | + */ |
| 68 | + |
| 69 | +\Magento\Framework\Component\ComponentRegistrar::register( |
| 70 | + \Magento\Framework\Component\ComponentRegistrar::MODULE, |
| 71 | + 'Magento_Backend', |
| 72 | + __DIR__ |
| 73 | +); |
| 74 | +``` |
| 75 | + |
| 76 | +- how registered when not in composer - project composer.json autoload/files[] = app/etc/NonComposerComponentRegistration.php [6] |
| 77 | +```json |
| 78 | + "autoload": { |
| 79 | + "files": [ |
| 80 | + "app/etc/NonComposerComponentRegistration.php" |
| 81 | + ], |
| 82 | + } |
| 83 | + |
| 84 | +``` |
| 85 | + |
| 86 | +Examples [1],[2],[3],[4],[5],[6] taken from [Magento DevDocs - Register your component](http://devdocs.magento.com/guides/v2.2/extension-dev-guide/build/component-registration.html) |
| 87 | + |
| 88 | +##### registration.php types |
| 89 | + app/code/*/*/cli_commands.php, registration.php |
| 90 | + app/design/*/*/*/registration.php |
| 91 | + app/i18n/*/*/registration.php |
| 92 | + lib/internal/*/*/registration.php |
| 93 | + lib/internal/*/*/*/registration.php |
| 94 | + |
| 95 | +##### registration.php load flow |
| 96 | +- pub/index.php |
| 97 | +- app/bootstrap.php |
| 98 | +- app/autoload.php |
| 99 | +- vendor/autoload.php |
| 100 | +- vendor/module[]/registration.php -- last step in Composer init |
| 101 | +- `Magento\Framework\Component\ComponentRegistrar::register(type='module', name='Prince_Productattach', path='/var/www/...')` |
| 102 | + |
| 103 | +# Describe module limitations. |
| 104 | +Minimization of software dependencies is a cornerstone of Magento architecture. Based on this principle there are several logical limitations: |
| 105 | +1. One module is responsible only for one feature. |
| 106 | +1. Module dependencies on other modules must be declared explicitly. |
| 107 | +1. Excluding or disabling a module should not disabling another module. |
| 108 | + |
| 109 | +# How do different modules interact with each other? |
| 110 | +Magento 2 is [PSR-4](https://www.php-fig.org/psr/psr-4/) compliant. As a main principle of module interaction Magento 2 declares a dependency injection pattern and service contracts. |
| 111 | +One of the important parts of module interaction is a Magento area. All components operate with the system and other components in the scope of default areas. |
| 112 | + |
| 113 | +### Module dependencies |
| 114 | +Modules can contain dependencies upon these software components: |
| 115 | +1. Other Magento modules. |
| 116 | +1. PHP extensions. |
| 117 | +1. Third party libraries. |
| 118 | + |
| 119 | +Module dependencies can be managed by: |
| 120 | +>1. Name and declare the module in the module.xml file. |
| 121 | +>1. Declare any dependencies that the module has (whether on other modules or on a different component) in the module’s composer.json file. |
| 122 | +>1. (Optional) Define the desired load order of config files and .css files in the module.xml file. |
| 123 | +> |
| 124 | +> -- [Magento DevDocs - Module dependencies](http://devdocs.magento.com/guides/v2.2/architecture/archi_perspectives/components/modules/mod_depend.html) |
| 125 | +
|
| 126 | +> Example: Module A declares a dependency upon Module B. Thus, in Module A’s module.xml file, Module B is listed in the <sequence> list, so that B’s files are loaded before A’s. Additionally, you must declare a dependency upon Module B in A’s composer.json file. Furthermore, in the deployment configuration, Modules A and B must both be defined as enabled. - [Magento DevDocs - Module dependencies](http://devdocs.magento.com/guides/v2.2/architecture/archi_perspectives/components/modules/mod_depend.html) |
| 127 | +
|
| 128 | +### Dependency types |
| 129 | + |
| 130 | +##### Hard dependency |
| 131 | +> Modules with a hard dependency on another module cannot function without the module it depends on. |
| 132 | +Specifically: |
| 133 | +>1. The module contains code that directly uses logic from another module (for example, the latter module’s instances, class constants, static methods, public class properties, interfaces, and traits). |
| 134 | +>1. The module contains strings that include class names, method names, class constants, class properties, interfaces, and traits from another module. |
| 135 | +>1. The module deserializes an object declared in another module. |
| 136 | +>1. The module uses or modifies the database tables used by another module. |
| 137 | +> |
| 138 | +> -- [Magneto DevDocs - Module dependency types](http://devdocs.magento.com/guides/v2.2/architecture/archi_perspectives/components/modules/mod_depend_types.html) |
| 139 | +
|
| 140 | +##### Soft dependency |
| 141 | +>Modules with a soft dependency on another module can function properly without the other module, even if it has a dependency upon it. Specifically: |
| 142 | +>The module directly checks another module’s availability. |
| 143 | +>The module extends another module’s configuration. |
| 144 | +>The module extends another module’s layout. |
| 145 | +> |
| 146 | +> -- [Magneto DevDocs - Module dependency types](http://devdocs.magento.com/guides/v2.2/architecture/archi_perspectives/components/modules/mod_depend_types.html) |
| 147 | +
|
| 148 | +Magneto module install order flow: |
| 149 | +1.The module serving as a dependency for another module |
| 150 | +2.The module dependent on it |
| 151 | + |
| 152 | +Following dependencies should not be created: |
| 153 | +>1. Circular (both direct and indirect) |
| 154 | +>1. Undeclared |
| 155 | +>1. Incorrect |
| 156 | +> |
| 157 | +> -- [Magneto DevDocs - Module dependency types](http://devdocs.magento.com/guides/v2.2/architecture/archi_perspectives/components/modules/mod_depend_types.html) |
| 158 | +
|
| 159 | + |
| 160 | +>You can build dependencies between classes in the application layer, but these classes must belong to the same module. Dependencies between the modules of the application layer should be built only by the service contract or the service provider interface (SPI). - [Magneto DevDocs - Module dependency types](http://devdocs.magento.com/guides/v2.2/architecture/archi_perspectives/components/modules/mod_depend_types.html) |
| 161 | +
|
| 162 | +### Magento areas |
| 163 | +A Magento area organizes code for optimized request processing by loading components parts which are related only to the specific area. Areas are registered in the `di.xml` file. |
| 164 | + |
| 165 | +>Modules define which resources are visible and accessible in an area, as well as an area’s behavior. The same module can influence several areas. For instance, the RMA module is represented partly in the adminhtml area and partly in the frontend area. |
| 166 | +If your extension works in several different areas, ensure it has separate behavior and view components for each area. |
| 167 | +Each area declares itself within a module. All resources specific for an area are located within the same module as well. |
| 168 | +You can enable or disable an area within a module. If this module is enabled, it injects an area’s routers into the general application’s routing process. |
| 169 | +If this module is disabled, Magento will not load an area’s routers and, as a result, an area’s resources and specific functionality are not available. |
| 170 | +> |
| 171 | +> -- [Magento DevDocs - Modules and areas](http://devdocs.magento.com/guides/v2.2/architecture/archi_perspectives/components/modules/mod_and_areas.html) |
| 172 | +
|
| 173 | +Magento has 5 areas types: |
| 174 | +>1. Magento Admin (adminhtml): entry point for this area is index.php or pub/index.php. The Admin panel area includes the code needed for store management. The /app/design/adminhtml directory contains all the code for components you’ll see while working in the Admin panel. |
| 175 | +>1. Storefront (frontend): entry point for this area is index.php or pub/index.php. The storefront (or frontend) contains template and layout files that define the appearance of your storefront. |
| 176 | +>1. Basic (base): used as a fallback for files absent in adminhtml and frontend areas. |
| 177 | +>1. You can also send requests to Magento using the SOAP and REST APIs. These two areas |
| 178 | +>1. Web API REST (webapi_rest): entry point for this area is index.php or pub/index.php. The REST area has a front controller that understands how to do URL lookups for REST-based URLs. |
| 179 | +>1. Web API SOAP (webapi_soap): entry point for this area is index.php or pub/index.php. |
| 180 | +> |
| 181 | +> -- [Magento DevDocs - Modules and areas](http://devdocs.magento.com/guides/v2.2/architecture/archi_perspectives/components/modules/mod_and_areas.html) |
| 182 | +
|
| 183 | +# What side effects can come from this interaction? |
| 184 | +- error when module is missing or disabled |
| 185 | +- error when injecting missing class |
| 186 | +- (?) null or error when using object manager for missing class |
| 187 | + ReflectionException - Class MissingClass does not exist |
| 188 | + objectManager->create() = new $type() or new $type(...args) --> PHP Warning: Uncaught Error: Class 'MissingClass' not found |
0 commit comments