Magento : Initial Setup Development Tips

1. Automatically turn on developer mode based on domain.


We have a stage site setup for all of our projects at stage..com. We also develop projects locally at either .dev or .local (based on developer preference). This snippet of code will only enable error reporting and Magento’s developer mode on stage or local domains.

Insert the following code after “require_once $mageFilename;” in index.php

# If the domain is ***.dev, ***.local or stage.**** then run site in developer mode and turn on error reporting
if(in_array(substr($_SERVER['HTTP_HOST'], -4), array('.dev', 'ocal') )
  || strstr($_SERVER['HTTP_HOST'], 'stage.')
){
	Mage::setIsDeveloperMode(true);
	ini_set('display_errors', 1);
}

 

2. Enable logging


Magento has a built-in logging method that allows you to log any variable type to a log file. This is very helpful when building Magento modules, as you can easily inspect data structures, without having to open a debugging session to inspect the variables in local scope. By default, logging is turned off in Magento. To enable logging, go to the “Developer” tab on the “System > Configuration” page. Change the “Enabled” select list under the “Log Settings” section to “Yes” and then save the page.

You can log variables to the Magento log using the following code: Mage::log($variable); By default, logs are stored in var/log/system.log

Here are some example usages of Mage::log()

 

# Log data from a Model
$model = Mage::getModel('catalog/product')->load(1);
Mage::log($model->getData());

# Log data from a Collection
$collection = Mage::getResourceModel('catalog/product_collection')->addAttributeToSelect('*')->load();
Mage::log($collection->getItems());

# Log data from array
$array = array('bar' => 'foo', 1, 2,);
Mage::log($array);

 

If you’re developing on a Mac, I’d recommend opening the system.log file with the Console app. If you’re on a *nix based machine, you “tail” the latest contents of the log file using the following bash command:
tail -f

/var/system.log

 

3. Enhance error backtraces


All of our developers use xdebug as a part of their Apache configuration. Xdebug has two main benefits: (1) It allows us to use PDT to debug PHP applications. (2) It overrides the default PHP error messages with detailed, fully customizable error backtraces. You can see an example backtrace below:

This is what that error would look like without xdebug:

 

Magento has built-in error and exception handling. Since errors are being handled by Magento, we have to modify a core file to make Magento let PHP/xdebug handle the displaying of the messages.

In a future blog post, will cover how to make Magento let xdebug handle exceptions

In app/code/core/Mage/Core/Model/App.php, replace the setErrorHandler() method with the setErrorHandler() method below:

  public function setErrorHandler($handler)
    {
        // HACK: Magento should only handle errors if developer mode is off
	if(!Mage::getIsDeveloperMode())
        	set_error_handler($handler);
	// END HACK
        return $this;
    }

Modifying core Magento files is never recommended, but if Magento gets upgraded and this change gets overridden, there won’t be an issue, since it’s not critical for the site to function.

Leave a Reply

Your email address will not be published.