In this article, we will speak about the best practices that should be taken into consideration when working with Magento 2, the latest version of one of the most popular e-commerce platforms on the market.
Indeed, this version is accompanied by a new philosophy, which advocates standards, conventions, and good practices. Magento 2 wants to be more mature and only wants to address professionals. This program is a training on best practices and how to develop a Magento 2 extension in the standards.
In other words, say goodbye to the somewhat dubious scripts, and say hello to the beautiful code!
1 – You will be attentive to the responsive design
You should not neglect mobile users! More and more customers are starting their purchases from a mobile. That’s why you have to rely on responsive design. If a visitor visits your page and it takes too long to load, it’s not adapted or unreadable on his smartphone (or on his tablet), you can be sure he will not come back to your site. By avoiding these bad experiences, you will not only increase your conversion rate but also your traffic!
2 – You will be in developer mode code
Nothing more natural than to activate the developer mode, to develop its extension! Developer mode will automatically disable the static file cache, enable automatic code compilation, and make debugging easier and more visible. Be careful, only activate the developer mode for your developments. This causes slowdowns and drastically reduces performance.
3 – You will use the injection of dependencies
Direct class instantiation is not recommended, the class you want to use may be overloaded by the DI of Magento 2. Indeed, the class instantiated directly does not take into account the overloads declared in the di.xml. This is why it is essential to use the injection of dependencies. In addition, it allows you to uncouple your code, avoiding strong inter-dependencies between your classes. Your code becomes reusable and can be used as a “service”.
4 – You will consider less legacy and more composition
The philosophy of Magento 1 and Magento 2 has changed dramatically on this subject. While in the 1st version, class inheritance is encouraged, he is neglected for class composition in his new version. But why this sudden change? This comes from an observation made by Magento’s technical team. It turns out that the composition is much easier to maintain when changing a class or when a feature is updated.
5 – You will write unit tests
If you are a regular test-driven-development ( TDD ), unit tests certainly have no secrets for you! If you do not, just say that Magento 2 favors extensions that have them. Not only does this ensure non-regression of the code and will force you to think deeply during your analysis, but it will increase the trust of developers who will use your extension. Indeed, it is a guarantee of quality and proves that the functional aspect of your extension is tested in its entirety.
6 – You will use the existing menus
On this point Magento 2 is uncompromising! The extensions will have to fit in the existing menus. Exit therefore multiple entries in the level 1 of the menu, as we could see on Magento 1. This means that your extension must be categorized to respect the navigation of Magento 2 not to lose the user. Thus, if you develop an extension on the products, it will be placed in the “PRODUCTS” menu. However, if your extension is exclusive and it implements something completely new, it will be placed in a section specifically designed for this purpose. However, the name and icon for this section are still not defined by Magento.
In addition – You will respect The Admin Style Guide
Magento 2 imposes its style! A list of rules specifies the formats you will need to use, including typography, iconography, layout grids, and the color palette. Magento 2 wants to standardize the Backend style to form a homogeneous whole.
Also, you will execute The Magento Code Sniffer Coding Standard, because what better than a tool to check that all the rules stated above have been respected? This tool, based on PHP Code Sniffer, will automatically check that your code does not contain errors and that it respects the different standards of Magento 2.
You benefit from having a code that corresponds to the Magento 2 standards and that will easily pass the technical review phase. This is simply to create code understandable by all, and therefore more easily maintainable!