Are you a Magento 1 developer jumping in to Magento 2? Want to know what’s different? Want to know what’s the same? This guide will help you.
NOTE: This is by no means the only things different from Magento 1 to Magento 2. This does highlight some of the major changes developers will encounter when moving to the new platform.
Tech Stack and Tools
Modern Tech Stack
Magento 2 uses a modern tech stack. This includes a modern version of PHP and MySQL.
Magento 2 requires:
- PHP 5.5.x, 5.6.x, 7.0.2
- MySQL 5.6
- Apache 2.2 or 2.4
Full requirements are here.
Magento 2 uses Composer for dependency management. Composer manages all external dependencies (frameworks, components) as well as module dependencies.
Read more about Composer and Magento here.
Code Coverage and Tests
Magento 2 attempts to have 100% code coverage through the use of Unit Tests. It utilizes PHPUnit for unit testing and the Magento Testing Framework for functional tests.
Read more about testing and Magento here.
Magento 2 uses PHP Standard Recommendations (PSRs) for coding standards and autoloading.
Read more about PSR here.
XML is still used in Magento 2, just as Magento 1. Magento 2 takes a much more strict and granular approach to XML.
All XML is validated against an XSD.
All XML is separated by function. This means that XML that creates routes is in a separate file than the XML that creates event observers.
All XML is separated by area. It does not make sense to load all XML for every request when it is not needed. In Magento 2, XML can be separated by area. This means that all XML that is designated only for the frontend will be loaded. The same goes for backend (adminhtml) XML.
Magento 2 is framework independent. Magento 1 was fully dependent, and leveraged the Zend Framework (version 1). Magento 2 is no longer married to a single framework. Magento 2 uses whatever components necessary to fulfill its goals. This includes components from many different frameworks. This also includes 3rd party libraries. Magento 2 uses Monolog, Symfony, Zend Framework, and many more 3rd party code.
All code pertaining to a module is now located in that respective module’s folder.
For example, in Magento 1, you needed a module declaration file in app/etc/modules/, template files in app/design/frontend/base/default/template/, and so on. In Magento 2, all of your files are located inside of your module. This makes it much easier to package, distribute, and maintain your code.
Read more about module structure here.
Magento 2 does not limit class types like Magento 1. When you created a class in Magento 1, you either created a block, model, helper, or library class. This is not true in Magento 2. Magento 2 allows you to create whatever class and class structure you need to accomplish your goals.
For example, Observers used to be in the Model/ folder inside your module in Magento 1. Now they are located in the Observer/ folder in Magento 2.
Modules can have dependencies, just like in Magento 1. Now, modules can also be dependent on external components, libraries, and code. You can specify this in your composer.json of your particular module.
Modular Code Organization
Magento 2’s modules are more granular and independent than Magento 1. For example, the Quote class was located inside of the Sales module in Magento 1. Now it is located inside of it’s own module, called Quote.
Plugins are a new form of customization for Magento 2. This allows you to perform changes before, or after a method on a class is called. This is a way to resolve rewrite conflicts from Magento 1.
Read more about Plugins here.
Rewrites are still possible with Magento 2. The key difference is how they are created. Now, you specify a preference for a class that is injected into another class.
Read more about Preferences here.
Observers are available in Magento 2. They have changed slightly in how they are written. They implement an interface ( ObserverInterface ). They are restricted to one observer per class. They are located in their own folder, (instead of the Model/ folder).
Read more about Observers here.
The API has been rewritten for Magento 2. It is no longer held in separate classes. Instead, already existing classes are used to fulfill the functionality of the API. This way there is no code duplication, or discrepancy between core functionality and API functionality.
Magento 2 uses dependency injection to resolve class dependencies in objects. This allows for easier testing, as well as easier dependency management between objects.
Read more about dependency injection here.
In order to fulfill functionality needed, Magento 2 automatically creates classes. This includes classes like factories, proxies, and interceptors. These classes are held inside of the var/generation folder.
Services and Repositories
Magento 2 now uses services and repositories inside of the platform. These are patterns that allow for stable upgrades, easier development, consistent code, and fulfillment of dependency injection principles.
Separation of Responsibility
Magento 2 now separates all responsibilities into different classes. For example, Controllers used to house all action methods within a single action controller. They are now separate.
The same applies with Observers. A single Observer class used to contain all observer methods for a module. All observers in Magento 2 are separated into their own classes.
Installation and Upgrades
Installation and upgrades are handled differently in coding organization. They are no longer handled by a bunch of version specific files. They are now handled inside of Installation / Upgrade classes for schema and data.
Magento now uses LESS for CSS precompilation. You it also provides the capability to swap it out for SASS.
Read more about Magento 2 CSS pre-processing here.
Built-In Command Line
There is built-in command line functionality inside of Magento 2. You can also add your own command line options to the command line tool.
Varnish caching is available out of the box.
Know Any More?
Was there something I missed? Left out?
Let me know in the comments below and I will add them to the list.