Initial Magento Setup Development Tips

Over the past year and a half of developing Magento sites, our development team has created a standard set of modifications/tweaks that we make to every Magento installation.

1. Automatically turn on developer mode based on the 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:

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.

4. Enhance exception backtraces

In this Enabling Xdebug’s Exception Handler in Magento blog post, you can read about how to modify Magento to let Xdebug handle exceptions.

Summary

Hopefully, these few tips will help you in your Magento development.  If you have any general tips for Magento development, I’d love to hear about them.

Share it

Topics

Related Posts

Google and Yahoo Have New Requirements for Email Senders

What ROAS Really Means

Everything You Need to Know About Updating to Google Analytics 4

Contact Us