It has been exciting to take a peek at the Magento 2 code now and then, and to view the changelog as new things are being added or modified. I figured it was about time to get a copy installed and create a module to start to learn the differences.
I have a simple developer toolbar module that I wrote that adds the bar to the top of the page, both in the admin and on the front end, that allows you to toggle logging, template path hints, profiler, cache, flush cache if on, and mark all unread admin notifications as read (if there are any). The module uses no template files at all and is all self-contained, so no matter what your design is, it always works.
The first noticeable difference is the way you call models, helpers, blocks, etc. No more shortcut, xpath-y style of calling things. You need to use the full class name now. Here are a few examples:
<?php // Magento 1.x Mage::getModel('core/cache'); Mage::getModel('catalog/product_type_configurable'); Mage::helper('catalog'); // Magento 2 Mage::getModel('Mage_Core_Model_Cache'); Mage::getModel('Mage_Catalog_Model_Product_Type_Configurable'); Mage::helper('Mage_Catalog_Helper_Data');
No More app/etc/modules/Your_Module.xml
That’s right, there are no more separate xml files for each module in app/etc/modules. Instead, you simply add the xml tags into the module’s config.xml file. At it’s most simple form, that simply means adding the
codePool tags into your config.xml:
<config> <modules> <Your_Module> <version>1.0.0</version> <active>true</active> <codePool>local</codePool> </Your_Module> </modules> ... </config>
No More Declaration of Module’s Models/Blocks/Helpers
In Magento 1.x, if you wanted to have your own model files for instance, you needed to declare that in your config.xml with code like this:
<config> <global> <blocks> <my_module> <class>My_Module_Block</class> </my_module> </blocks> <models> <my_module> <class>My_Module_Model</class> </my_module> </models> <helpers> <my_module> <class>My_Module_Helper</class> </my_module> </helpers> </global> </config>
There is no longer any need for this due to the using of full classnames when calling models for instance. This really simplifies things and helps remove some room for error when writing modules.
Many Events Have Changed
The other difference is that the event that I was observing was no longer an event. I was extremely surprised to find that there was a different event that provided a LOT more flexibility and a LOT more options for doing cool things with it. I initially was frustrated when I found out that my observer wasn’t firing, but was thrilled to find out that Magento only made a way better option. So, as you begin to experiment and port your modules over to Magento 2, be slow to get frustrated. A lot has changed, and so far from my experience, it has been for the better!
I will continue to post more as I experiment with Magento 2.