Why is c
lowercase in controllers
folder name? Whereas Model, Block and Helper are starting with an uppercase letter?
-
7Hey...why the downvote? This is a legit and actually good question.– Marius ♦Commented Nov 6, 2014 at 7:53
-
There's a member (I will not tell the name of course) who is flagging/downvoting randomly. For example this question was flagged as offensive :D– Tim BezhashvylyCommented Nov 6, 2014 at 9:12
-
@TimBezhashvyly...yeah I understand that. It seamed offensive to me at start but then I read the question again :)– Marius ♦Commented Nov 6, 2014 at 9:26
-
Controller with a capital C was already taken haha.– Julien LachalCommented Nov 6, 2014 at 9:43
2 Answers
The classes located in the controllers
folders are a special breed of classes.
You cannot rewrite them in the same way as you rewrite a model or a block using the <rewrite>
tag in config.xml
, you cannot instantiate them using a factory like you do for models (Mage::getModel()
) or with helpers (Mage::helper
) or with blocks (Mage::app()->getLayout()->createBlock()
).
I mean there is Mage::getControllerInstance()
but that is a bit different. More on this later.
You may notice that they don't actually follow the naming rule as the other classes.
The work controllers
is not found in the class name.
Let's take for example the controller found in Mage/Catalog/controllers/CategoryController.php
.
The class name is Mage_Catalog_CategoryController
.
I cannot give you a 100% certain answer, only a core dev can do that.
But my assumption is that someone did not want the controllers to be autoloaded.
Take a look at this method Mage_Core_Controller_Varien_Router_Standard::match
. It's big and scary, but that's the one that maps an url to a controller and an action.
There are a lot of calculations done but somewhere there is this line:
$controllerClassName = $this->_validateControllerClassName($realModule, $controller);
If we dig deeper in the _validateControllerClassName
you will end up in getControllerFileName
that looks like this:
public function getControllerFileName($realModule, $controller)
{
$parts = explode('_', $realModule);
$realModule = implode('_', array_splice($parts, 0, 2));
$file = Mage::getModuleDir('controllers', $realModule);
if (count($parts)) {
$file .= DS . implode(DS, $parts);
}
$file .= DS.uc_words($controller, DS).'Controller.php';
return $file;
}
and in _includeControllerClass
that basically does this: include $controllerFileName;
.
Notice the hard coded controllers
in the method getControllerFileName
and notice that the controller file is just included. So no autoloading.
Finally, the controller factory Mage::getControllerInstance()
does not locate the class and does not look in the rewrites. It just does return new $class($request, $response, $invokeArgs);
where $class
is the controller class.
Side note: In some modules there is a folder called Controller
(with capital C) and the classes in follow the standard class naming. Those classes are not really controllers. They are used as parent classes for other controllers in the module or as routers.
Side note 2 : This does not exist in Magento 2. All controllers are located in the Controller
folder.
Marius you are so great :D
My answer would just have been:
This is Zend Framework standard: http://framework.zend.com/manual/1.12/en/zend.controller.quickstart.html
-
1
-
1this is a beautiful answer. I think this would be more correct answer than Marius. Because a zend framework developer wouldn't think about another directory other than
controllers
and I assume Mage core team would be a zend framework experts. Kudos. Thanks for sharing that link Commented Nov 6, 2014 at 9:05 -
-
1Yep I think that this is the correct answer as well. I didn't even bother to look at the ZF standards. I just tried to find a logical explanation. In my defense, I stated in the answer that it was my opinion.– Marius ♦Commented Nov 6, 2014 at 9:28