Magento 2 Concepts that Every Developer Should Know

Magento 2 Concepts that Every Developer Should Know

Magento has evolved with time, giving us better code base, more structured modules, and easy components to build an eCommerce site. The learning curve, however, has remained the same for Magento 2. The new version that was released sometime in 2015 comes with some of the most amazing features that prove to be developer friendly, and have something exquisite to offer to the users.

Magento 2 delves into some amazing features that you should know if you want to leverage this platform, and build something unique for your clients. We will take you through the 2.0 concepts that you as a developer should be aware of.

Module Structure

Composer, the PHP dependency manager, is an integral part of Magento 2. Long back, Magento relied on a single core codebase, which has now been transformed into individual components that can be installed via Composer. You can use this module structure for third-party libraries as well as the core modules/themes. Magento has introduced package dependencies that can allow you to express the different Magento versions in the form of components and their dependencies.

A Magento developer can easily create composer packages, which consist of self-contained components that can enhance the Magento structure, thus making customization or extension development easy. The files for Magento 2 are placed in a single directory, which was definitely not the case for Magento 1.The main directory and sub-directory view make finding things easy.

Dependency Injection

This is one of the most talked about concepts, one that has done a huge favor to Magento 2, by making it developer friendly. It is a huge topic and requires an article in itself. Here we will try to take you through a few brief concepts. In the previous version of Magento, the factory methods such as class overrides Mage::getModel and Mage::helper were used. In the second version, it is more structured, and here the external types are injected into a parameter within a class constructor. These types that are referenced are auto-initiated and they are then transferred to the Magento Object Manager. Here real class names and PHP namespaces are used, thus organizing the code, centralizing the references and allowing a more flexible approach. Interfaces play a pivotal role in Magento 2, and dependency preferences help in implementing these interfaces. The dependency injection configuration is how third party modules change the preference for default interface or the entire class in a global format for the Magento 1.x.

Code Generation

One of the biggest challenges for Magento Programmer would be the idea of static files located in the different locations. This is where automated code generation combined with deployment in Magento 2 comes to play. The pub directory is where the required web root, which is located away from application code, and contains what a developer, wants to add to it such as the static view files, and media. You have more control on the site code as it is away from the web root, and makes deployment or compilation easy.

The static view file deployment includes all static assets from the source, and deploys it to the web accessible pub directory. This is automatic, and the deployment occurs in development mode, wherein the JS and CSS code stay in the appropriate module, and can be accessed from centralized location.

Certain PHP classes are auto-generated with Magento, while some others involve certain methods that are distinct from pub directory.

Plug-ins

The concept of plug-ins works different with Magento 2. With this new version, you have some excellent techniques specifically for customization, which have been facilitated by code generation and plug-ins.

With plug-ins, you can alter or extend the targeted methods, thus saving the time that goes into replacing entire classes.

The plug-ins are defined using di.xml files, and they come with essential “before”, “after” and “around” implementations that allows you to change the input method that existed originally, and work on the output in a way that defines your logic. You can use a single method to define multiple plug-ins in the new version, which improves compatibility within the extensions. The code generation and object manager help you with the method calls by running them along the right places, and then placing the plug-ins as and when necessary. The idea is to improve development pace, and store performance.

Caching

The full page caching in Magento 1 was related to performance enhancement, and was available only with the enterprise edition. Customizing while accommodating for full-page caching required complete understanding and support from developer. In Magento 2, the full-page caching has evolved and is available for both enterprise and community edition. It is more developer friendly, and is present in the Magento_PageCache class.

Full-page caching is challenged when it is to be applied in places where the web page content cannot be universal for visitors and page views. In the previous version, assembling content handled this challenge, but the new version has a unique take towards the situation. The page blocks are de-coupled from the main caching content, and they are defined as “private data”, and this data is dependent on visitor’s session information. These blocks are then made anonymous for all visitors during the initial rendering, and the private data is loaded using Ajax request, only after the main page content is made visible. This ensures caching of the main body, thus making web page delivery swift and easy.

CSS Compilation

CSS got a responsive overhaul in the Magento 1.x itself, which became SASS/Compass post the overhaul. In case of Magento 2, the tool that will be used in place of SASS will be LESS, which comes with similar capabilities such as CSS variables, mix-in styles to ensure repeatability, and multi-file organization capabilities. For the Magento 2 developer compilation would be a more visible process. You will need to first ensure that the LESS source files are in place, and their location matches the need. Magento will do the rest. Compilation during development is easy with the new version. The _module.less, and _widgets.less allow cumulative use of style, and don’t override the partials that are named in an identical manner.

Magento 2 has evolved to become developer friendly, and offers easy and automated code generation. Developers need to get a hang of these concepts that have been discussed here, so as to maximize the potential of this framework, and get the most out of it.