Debugging Magento
20.06.2014
Angelika Siczek
Below is a brief statement prepared by one of our programmers – Paweł on how to debug the Magento code, in case we want to read errors appearing in the internet store or draw individual variables or methods.
We turn off the cache
We reindex everything
We clean the browser cache
We enable Magento logs:
System> Configuration> Developer (Developer)
In the log settings, switch to on. By default, logs are saved in the path: var/log
Enter the index.php file and uncomment (or append) the lines
ini_set (‘display_errors’,1);
Enter the .htaccess file and switch Magento to the development mode by adding the code:
SetEnv MAGE_IS_DEVELOPER_MODE “true”
We change the name of the errors local.xml.sample file to errors/local.xml
Additionally, we install ‘Easy Template Path Hints‘ – an add-in that displays block paths and views on the front and backend.
When we write the code, sometimes it will be necessary to display different variables. You can do this by simply displaying them: echo($variable). However, this function will not display either an array or an object. To display an array, you can use print_r($variable) or var_dump($variable). Magento is based on Zenda, therefore we can use Zend_Debug:: dump($ variable).
In order to check what class we are in, the get_class function is useful. E.g. var_dump(get_class($this));
Similarly, we can check what fields we have and the methods of the class we are in get_class_vars and get_class_methods.
Sometimes you may need to display a variable on the shop’s production version. We can then wrap our dump in a div with display none:
<div style=”display: none;”><?php var_dump($variable) ?></div>
Another method can be to display the variable only for our IP:
if($ _SERVER[‘REMOTE_ADDR’]==’11.11.11.11′){
Zend_Debug::dump($variable);
}
When we cannot throw a dump on a page (e.g. in some controllers or e-mails), we can put it in our log with file_put_contents. In addition, if it is an array, we use the var_export function.
file_put_contents(Mage::getBaseDir().”/var/log/moj.log”, $variable);
file_put_contents(Mage::getBaseDir().”/var/log/moj.log”, var_export($array));