Custom module development: basic errors

Custom module development: basic errors

Magento custom module development has tricky parts that are not obvious for inexperienced developer. For example:

  • you have created a router, but get 404 page
  • you have created a layout, but page content is empty
  • you have added new block to layout, but page is still empty

There are no errors on frontend and logs are empty. That makes debugging much complicated.

In this article you will find out how to resolve such issues.

1. You have created a router and a controller, but get 404 page

Most common mistakes are

  • controller class file has wrong name
  • route cannot be resolved to class name
  • wrong class name

It could be just a typo. It also might be that you do not understand method of matching controller name and action name. In this case please refer to Magento Knowledge base.

So how to check controller path in magento? Open Mage_Core_Controller_Varien_Router_Standard ( located in app/code/core/Mage/Core/Controller/Varien/Router/Standard.php ) and look for protected function _validateControllerClassName

protected function _validateControllerClassName($realModule, $controller)
{
   $controllerFileName = $this->getControllerFileName($realModule, $controller);
   die($controllerFileName); // this line will terminate the app and output controller file path
   if (!$this->validateControllerFileName($controllerFileName)) {
       return false;
   } 
	...
}

If the path contain Mage/Core/… instead of Namespace/Modulename it means that something is wrong with the router in config.xml. And again please refer to Magento Knowledge base.

After the router is fixed you could get such errors as:
“Controller file was loaded but class does not exist” – there is a typo in controller class name
“Fatal error: Call to undefined method NameSpace_Modulename_IndexController::hasAction()” – controller is not extends from base frontend controller Mage_Core_Controller_Front_Action or base backend controller Mage_Adminhtml_Controller_Action.

2. I have router, controller and method that return “Whoops, our bad…”

It means that controller class missing method. Check your method name, perhaps you just miss Action in the end.

3. I have created layout file, but it is not working.

Most simple way to check that your file is loaded is to broke xml structure and make it not valid

<QWE?xml version="1.0"?>

Reload the page and check if there is load error. If you do not see such message file is really not loaded. You need to check layout sections in config.xml, check xml file location.

If you want to check list of loaded xml files, you can use such code. Open /lib/Varien/Simplexml/Config.php and find loadFile method.

public function loadFile($filePath)
{
   if (!is_readable($filePath)) {
       //throw new Exception('Can not read xml file '.$filePath);
       return false;
   }

   /* added lines */
   $xmlLog = fopen("/full/path/to/magento/var/log/xml.log", "a");
   fwrite($xmlLog, "XML file: $filePath\n");
   fclose($xmlLog);
   /* added lines */
   
   ...

}

Leave a Reply

Your email address will not be published. Required fields are marked *