Wrote My First Magento 2 Module

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.

Calling Models/Blocks/Helpers
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:

// Magento 1.x
// Magento 2

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 active and codePool tags into your config.xml:


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:


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.

This entry was posted in Magento. Bookmark the permalink.

6 Responses to Wrote My First Magento 2 Module

  1. Vikram Purohit says:


    In Magento Ver 2.0 modules are placed in the ‘community’ code pool instead of local code pool. Is it true ? And what If I place my module in local code pool ? Will it violate any standards ?

  2. Bev says:

    It’s amazing in favor of me to have a website,
    which is valuable in support of my knowledge. thanks admin

  3. Pingback: Magento 2 - When it's coming? | Magesoft.at Blog

  4. Mark Shust says:

    Thanks for the write-up, good stuff

  5. KoKoC says:


    You can also use a static tests located in dev/tests/static to help identify some incompatible places between Magento versions.

  6. Vinai says:

    Hey Josh, nice post! Which event is gone and what is the new one? Can you give a little more meat please?? Thanks!