Patrick Ward words, code, and music

Magento Development Environment

Each time I have to work with Magento, I take a few minutes to set up the development environment. These steps ease some of the pain when Magento doesn’t work the way you expect it to and you have to dig down into troubleshooting mode.

Turn On Developer Mode

Magento has an IsDeveloperMode flag that overrides Magento’s default handling of errors. In normal mode, Magento will handle exceptions by logging the exception to a report under /var/report and then redirect the browser to a page that display’s the report id. This is great if you’re in production, but when you’re actively developing an extension or working on a site, you need immediate access to the error that ocurred.

Thankfully, the index.php file within the Magento root let’s you turn Developer Mode on. When active, Developer Mode tells Magento to skip the report writing and display the exception directly to the browser.

However, in the most recent version of Magento (v1.7.x at the time of this article), Magento also comments out the PHP directive display_errors and lists it separate from the server flag that turns developer mode on.

So, I modify index.php to include the display_errors directive if the MAGE_IS_DEVELOPER_MODE server flag has been set:

<?php
if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) {
    Mage::setIsDeveloperMode(true);
    ini_set('display_errors', 1);
}

Then, I modify .htaccess to enable/disable developer mode via the MAGE_IS_DEVELOPER_MODE flag:

SetEnv MAGE_IS_DEVELOPER_MODE "true"

Turn on Logging

Magento has some built-in logging available through the Mage::log function. However, you must turn logging on from within the admin panel before logging will work.

To turn logging on within the admin panel:

Configuration -->
  Developer -->
    Log Settings -->
      Enabled = Yes

Now, you can log messages to var/log/system.log:

<?php
Mage::log($foo);

Or, to a custom log file of your own (e.g. var/log/custom.log):

<?php
Mage::log($foo, null, 'custom.log');

Notes on Debugging

While we’re discussing development modes, it’s useful to note that there are some additional debugging functions available while you’re developing.

Most notably, the Zend_Debug::dump function is avilable, which can be used instead of var_dump or print_r to print objects to the screen:

Zend_Debug::dump($var, $label = null, $echo = true);

You should also be aware of the Varien_Object::debug and Varien_Object::getData methods, which can be used to present an object as a string or retrieve data from the object respectively. Used in cooperation with the Zend_Debug::dump function, they can be quite powerful:

Zend_Debug::dump($var->getData(), $label = null, $echo = true);