Surely every developer of modules for Magento got into a situation where everything seems to be done correctly, but does not work. This is especially true of typical situations: he created a router, but issues a 404 starnitsa, created a layout, but the content is not displayed, created a template, and the page is still empty. A distinctive feature of such a situation is that, as a rule, Magento does not react to them at all (no errors are issued, the logs remain empty). It is about debugging such errors and will be discussed in this article.
Having had enough of these situations, I gathered a small collection of design tips and module debugging methods that allow you to quickly deal with such troubles. They will be especially useful for beginners, but experienced developers can find something new for themselves.
So let's go!
I created a router and controller for it, but Magento shows 404 pages.
Why it happens:
Most likely, the wrong file is named in which the controller class is located, the path does not match the class name, the wrong class name, and similar errors. Perhaps this is just a typo due to inattention, but it may happen that you do not fully understand how Magento translates the request into frontName, controllerName and actionName. In this case, I recommend reading the Alan Strom
article on determining the name of the controller.
')
The only clarification that should be made for beginners is how to enter the name of the controller in the address of the page, which lies not in the controllers folder, but in its subdirectories. It's pretty simple, but sometimes problems arise.
So, you need to take a piece of the controller name between “Namespace_ModuleName_” and the word “Controller” at the end and bring it to lower case. For example, for the name Namespace_ModuleName_Subfolder1_Subfolder2_IndexController, the name of the controller in the address line will be the following subfolder1_subfolder2_index.
How to debug:
So, you have a good understanding of how the controller name is determined, but you still see 404 pages. If this happens, then most likely you put the file with the controller in the wrong folder or incorrectly named the file itself. The easiest way to check this is to bring the path to the controller that Magento is looking for. To do this, go to the Mage_Core_Controller_Varien_Router_Standard class and look for the _validateControllerClassName () method there. In this method, we get the full path to our controller, it remains only to withdraw it and check with ours.
Here is the corresponding code:
protected function _validateControllerClassName($realModule, $controller) { $controllerFileName = $this->getControllerFileName($realModule, $controller); var_dump($controllerFileName); die();
Now, instead of 404 pages Magento will give way to our controller. We fix and rejoice.
If the path contains Mage / Core / ... instead of your Namespace / ModuleName, then you did not define the router in config.xml or did it incorrectly. How to do it correctly we read everything in the same
article by Alan Strom.
Then we can expect several more displayed errors: namely, the Controller file was not loaded but it means that we typed the controller name incorrectly in the file or Fatal error: Call to undefined method NameSpace_TestModule_IndexController :: hasAction (), which says to us that we forgot to inherit the base controller Mage_Core_Controller_Front_Action for the frontend and Mage_Adminhtml_Controller_Action for the backend.
I created a router, controller and method, but I get a Whoops page, our bad ...
Why it happens:
Very rare and easy to fix error. It means that our controller does not contain the method you request.
How to debug:
Check the name of your method, most likely it is named incorrectly, maybe you forgot to add an Action after the method name.
It seems to me that one of the xml files is not loaded, or I want to see which files are being loaded.
Why it happens:
The reasons why it can not load the xml-file a lot and it makes no sense to consider them in this topic. The solution for all cases is the same, so we will go straight to it.
How to debug:
The simplest way to check if a particular xml file is being loaded is to make it invalid. Let's say add an extra unit to the file:
<1?xml version="1.0"?>
Now, when entering the page, you will see an error message about the loading of the xml file. If this message does not exist, the file does not load. What xml-files and why they can not be loaded is the topic of a separate article and will not be considered here.
The second part of our question is to find out which xml files are loaded. One way to do this is to add a few lines to the file lib / Varien / Simplexml / Config.php. We are looking for the loadFile method, which is responsible for loading all xml files, and appending several lines there:
public function loadFile($filePath) { if (!is_readable($filePath)) {
Do not forget to enter your path from the root of the server to the directory with installed Magento. And also create a file xml.log, at the specified path. Now the list of all xml files uploaded when the page is requested will be written to the specified log file.
You should not keep these lines enabled all the time, as the log will very quickly reach impressive sizes. You can simply comment them out until they are needed again.
I connected the file with the layout, added the handle with the block, but the page is still empty
For starters, I'll give you some code:
app/code/local/TestCompany/TestModule/etc/config.xml <?xml version="1.0"?> <config> <frontend> <layout> <updates> <testmodule> <file>testmodule.xml</file> </testmodule> </updates> </layout> </frontend> </config>
Here we connect the file with the layout. Here is the file itself:
app/design/base/default/layouts/testmodule.xml <?xml version="1.0"?> <layout version="0.1.0"> <testmodale_index_index> <reference name="root"> <action method="setTemplate"><template>page/2columns-right.phtml</template></action> </reference> <reference name="content"> <block type="core/template" name="testmodule.mainpage" template="testmodule/page" /> </reference> </testmodale_index_index> </layout>
This file contains several common errors, but we will discuss them a little later. In the meantime, suppose that everything is written correctly in this file. Also create a file with a template:
app/design/base/default/templates/testmodule/page.phtml <p>Test content.</p>
So now the reasons for this clean page and their solutions:
- You forgot to specify in the controller method that you need to load the layout.
The controller should contain the following lines of code:
$this->loadLayout(); $this->renderLayout();
- The file with the layout is not loaded.
How to check this version you already know from the previous paragraph.
- Wrong name of the handle in the file with the layout.
This is one of the mistakes that were made in our example. In the example, this is just a typo, you need to fix testmodale to testmodule, but sometimes it happens that you incorrectly determined the handle for a page. The simplest solution is to display all the handles whose contents Magento loads for the current page. To do this, add the following line of code to the controller method:
$this->loadLayout(); var_dump($this->getLayout()->getUpdate()->getHandles()); die(); $this->renderLayout();
For our example, one of their handles would be testmodule_index_index, which we specified.
- Incorrect title or template path.
This is the second mistake made in our example. The author of the code forgot to specify the .phtml extension for the file with the template. To debug such problems, you should include the so-called Template Path Prompts. This can be done in the administrative part of Magento. Go to System -> Configuration -> Developer. Select in the upper left corner of the Main Website. Now two additional fields appear in the Debug tab. We need to enable Template Path Hints. Now, when entering the page, we see the path that the template is in, which Magento connects to the page.
Of course, these are not all the possible problems that may arise when developing a module for Magento, but at least the most frequent and incomprehensible for newbies now, I hope, will not be so incomprehensible. Do not forget to share your tips in the comments.