What’s in a block? – Some Magento “basics”

Posted on 22. Jan, 2009 by Fido in Design, Development, Magento, Module

Many developers are familiar with the MVC (Model View Controller) design pattern that is seemingly ubiquitous throughout web frameworks. Looking through the code in Magento, however, we see many other components besides the M’s the V’s and the C’s.

Each module  (a “module” meaning the separate directories within the “app/code/core/Mage” directory that comprise of Magento’s different functional areas) contains the usual Controller and Model. You’ll see that within each module, there are no Views (more on this later). You’ll also see extra tidbits, such as “helper” , “etc” and “sql”. These are (and are not) standard within the Zend Framework context and will not be discussed in this article. In these modules are also the sort of files which we work with very often. The all powerful block! This article will attempt to (hopefully accurately) describe just what a Block is and how it’s used.

A top priority of (good) Object Oriented Programming is the decoupling of code. In a nut shell, this means that code should have the least amount of dependency on other code as possible. (Slightly more accurately, objects should not rely too heavily on other objects to exist and function). Blocks are part of Magento’s solution to keep their application loosely coupled.

A quick primer on MVC: When a page in Magento is called, the URL tells Magento what code to run. This is done via a “router” which calls upon a selected Controller to do it’s thing. The URL gets “routed” to a particular Controller, which in turns tells Magento what to do. The Catalog controller, for instance, is responsible for telling Magento to load a product collection and and then show those products to us. The Controller tells Magento which layout is to be used. This determines which modules are put into place, which in turn tells Magento what Views to output. The data from the Models are given to the Views to be displayed. In the scheme of things here, Blocks fit roughly between the View and the Model.

Note: The above paragraph tells a simplified version of how Magento uses Controllers (and of course there are exceptions). Magento is capable of doing much more work in the Controller than it generally does, but usually chooses not to as part of its attempts to decouple its code. Hopefully this becomes more clear to you why.

Magento has sets of modules (as mentioned above). Modules act as their own little entities, each (mostly) containing their own M’s V’s and C’s (athough the Views [phtml files] are located elsewhere). These modules create the separate areas within a typical Magento page; the header, header links, mini cart, recently viewed products, static HTML blocks, footer, and so on. Basically, comprised within any given web page are multiple modules, each of which call their own Views.

On any given page request, Magento needs to be able to show a certain set of modules (and by extension, those modules’ Views). The possibility of many sets of modules to be displayed on any given page means a decoupling is necessary amongst the Controller (which handles the page request), the Models/Blocks and their Views. This is where Blocks really shine. Blocks enable this decoupling by allowing the module to get it’s data from its Models, and pump out the data to the Views which display the relevant data. Each functional area of a page can then act separately. In this way, we can decide when we want to show a certain functional area (such as the Minicart). We can tell it to show up on product pages, but not CMS pages. We can move it from the right to the left column, to the center, to the header,to  the footer, or to a defined area we create!

So, what is a Block exactly? A Block is basically a data resource for Views. It uses the sites Models, grabs the data, makes any necessary adjustments, and then makes that data available to a View. For instance, when you override / create a Block that calls a product collection, that product collection is pulling some products’ data from a Model. The Block is where you create such a product collection and implement the product collection’s methods, such as adding filters or ordering the products in a certain way. That product collection is then made available to your View so you can display the products on a page.

Whew, this was a long article! And definitely not the usual we see here at Explore Magento.

In this article, we discussed a basic overview of MVC, how Magento implements MVC, modularity within Magento, decoupling of code (and the design-driven reason behind it’s decoupling), and the Block’s place within the architecture of Magento. We concluded that a block is basically a tool which allows Magento to place content items / functionality throughout the site in a modular way (in any given area and on any page).

I hope you found this article to be clear and useful! It was definitely a digression from our usual posts which aim at having the most bang for your buck.

Tags: , ,

19 Comments

JD

26. Jan, 2009

thanks! very helpful. A diagram (or maybe a link to the Magento sequence diagram) would make it killer.

Matt

03. Feb, 2009

Finally someone discussing the perils of skinning in Magento. We’re going grey trying to figure this bad boy out!! :)

Going away to read a lot now.

John

15. Feb, 2009

TERRIBLE.

This article is incoherent, not focused at all, and contains no valuable information.

the sad thing is that this is the best one out there.

:-(

Fido

15. Feb, 2009

Arg – Flash backs to High School English class! This was an admittadly late night post after digging around Magento code to get a better grasp on the inner workings of how Magento executes layout.
Anyway, some more concrete posts to come!

tom

04. Mar, 2009

thanks a lot for the article!

shahriat

26. Mar, 2009

awesome! really its a nice concept adding block logic with MVC design pattern. MVC design pattern was walking alone but now with block we can say MVC as MV+BC walking together, isn’t it :)

Greg

02. Jun, 2009

What is the difference between a block and a controller? A controller also sits between a model and a view, deciding what data from the model to make available to the view.

Yes, it also does routing, but I’m still not seeing the great value of blocks.

The only thing I can think of from what you have described is that maybe they work as nice ‘filters’ of data in the model for your views. A particular data transformation you might apply to a model. That could be kind of handy, I guess.

Fido

02. Jun, 2009

Routing is actually still determined by the URL / Router. The main difference with Magento, however, is that the router sets a certain key term based on the URL. This key term determines which blocks are (or aren’t) going to be displayed.

The basic flow for any request is this:
1) URL entered
2) Controller chosen and ran based on the Route. All Controllers in Magento begin the layout (template) process (Their base classes handle this).
3) Based on the URL the key term mentioned above is selected.
4) The layout is built -> Magento runs through the layout xml files and finds the blocks referenced in the XML. It runs whatever parameters or code on the block based on the XML.
Each block is responsible for it’s portion of HTML output. (It’s corresponding .phtml file).

The value of blocks that we see is the resulting modularity within Magento. This layout system allows users to create their own modules within this complex system without being disruptive to other code. You can have it interact with other portions of the site or be it’s own entity.

Controllers within Magento are all used as processors to user interaction. They DO initialize and kickoff the layout process, but don’t do the large bulk of work of actually building the layout (In a basic Zend Framework install, the Controller is responsible for making data available to the View, which is not the case in Magento).

The entire layout system in Magento, while very complex, is completely geared towards the extensibility of Magento.

You can definitely make an argument about Magento being over-engineered and not friendly to non-programmers (I sincerely hope Magento follows through on their promises of further documentation).

Tyler

11. Jun, 2009

Thanks for the information. I felt this was very informative and wasn’t too much. It seemed to contain the right amount of information.

teemso

28. Jun, 2009

Thanks for the information.

Ritesh

21. Jul, 2009

This is the best one

SG1

21. Aug, 2009

This article has helped me a lot, thanks.

Hope we’ll be able to have more detailed technical info from Magento Company.

As posted before, some Magento-MVC diagrams will improve our understanding for sure. ;)

Alex S.

25. Sep, 2009

Thanks for the information, it helped a lot. I wish there were more articles like this out there.

PHP Development India

08. Oct, 2009

This is a awesome article. From this you can get easily idea about block and working of that in magemto..

key

11. Nov, 2009

Great and useful !!!

Samuel M.

12. Dec, 2009

Thanks for the posting, I feel like Greg above as it’s still kind of fuzzy, but definitely better than no knowledge at all.

erfan

16. Dec, 2009

Its a very good article, BUT it stil gets you nowhere.. And thus its kinda pontless.. We need actual code of how to program the MCV..

James

08. Apr, 2010

My friend wrote this article for me on blocks and it was pretty useful

http://fishpig.co.uk/2010/04/08/blocks-in-magento/

It’s targeted more at people just starting Magento but is still good

Robin

23. Apr, 2010

Article is good and i got an idea about the Block concept. Thank you

Leave a reply