30

Why is c lowercase in controllers folder name? Whereas Model, Block and Helper are starting with an uppercase letter?

Marius
  • 197,939
  • 53
  • 422
  • 830
Muthu
  • 303
  • 2
  • 4

2 Answers2

30

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
  • 197,939
  • 53
  • 422
  • 830
16

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

Fabian Blechschmidt
  • 35,388
  • 8
  • 75
  • 182
  • 1
    It's actually a good answer. – Marius Nov 06 '14 at 08:40
  • 1
    this 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 – Rajeev K Tomy Nov 06 '14 at 09:05
  • Very help fulll – Amit Bera Nov 06 '14 at 09:25
  • 1
    Yep 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 Nov 06 '14 at 09:28