4

I'm reading the PHP Developer's Guide and I see this:

Blocks are used in Magento to reduce the burden on the controller by moving most of the logic into blocks.

What problem does this solve? Controllers can handle all the logic in every other MVC architecture. What limitation does Magento have that we can't "burden" the controllers with doing exactly what they are designed to do?

8

Controllers can handle all the logic in every other MVC architecture

Just because they can, doesn't mean they should. "Fat controller" vs "Skinny controller" is an old argument but at least in applications the scale of Magento, the skinny controller approach is in favor. There, controllers just determine what to do based on the request and leave the how to a service layer which contains the actual business logic.

Benefits are: More modularity, more flexibility, less procedural and more object oriented code.

doing exactly what they are designed to do

This is a weak argument, controllers are not designed to "handle all the business logic", at least this is a very questionable take on them and not a commonly accepted one.

  • Can you expand on how this leads to less procedural and more object oriented code? I'm coming from a RoR background, so I'm used to the skinny controller idea, but I'm not able to think of an analogous structure to blocks. – jmargolisvt Aug 14 '16 at 19:49
  • Then maybe I did not understand you correctly. Procedural: a controller that looks like "do this, then do that, then render these templates". Object oriented: (Magento style) load a dynamic hierarchy of blocks (the "layout") and let the blocks decide what to show and and often also where to get their data. The layout is a structure of objects that all play their own part in generating the page, which is more OOP than having one controller orchestrating everything step by step. – Fabian Schmengler Aug 14 '16 at 20:14
  • Related question/answer that might help you understand the purpose of blocks: magento.stackexchange.com/questions/74111/… – Fabian Schmengler Aug 14 '16 at 20:16

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.