Getting Started with Magento Development

Nurul Ferdous

December 2010


Magento 1.4 Development Cookbook

Magento 1.4 Development Cookbook

A practical guide to take your Magento store to the next level by developing powerful modules and extensions

  • Develop Modules and Extensions for Magento 1.4 using PHP with ease
  • Socialize your store by writing custom modules and widgets to drive in more customers
  • Achieve a tremendous performance boost by applying powerful techniques such as YSlow, PageSpeed, and Siege
  • Part of Packt's Cookbook series: Each recipe is a carefully organized sequence of instructions to complete the task as efficiently as possible
        Read more about this book      


Most probably you have heard the hype about Magento. It's an award winning open-source eCommerce stack with smart features such as layered navigation, auto-complete search, multiple language support, multiple stores, smart browsing, RSS product feeds, tagging and reviewing products, reviewing search terms, reviewing customer tags, poll manager, currency exchange rates, Google sitemap integration, abandoned shopping cart report, catalog-wide product comparisons, product wish lists, and even zooming the product image. It was created by Varien, based on the top notch MVC framework—Zend Framework, on March 31, 2008. As with other MVC applications, Magento keeps the display logic separated from the application's business logic. A dedicated team works on updating Magento regularly.

Magento Developer is a very hands-on role for both a novice and experienced software engineer who is interested in achieving high impact in a fast-paced development environment with an ambitious mission. Magento gives a very handy way to deal with its features faster than any other alternatives, whether you know a little or a lot about PHP programming. Let's see how these things happen.

We will make our development platform ready to cook some mouth-watering recipes for Magento in this article. If you are a newbie, this is the right place to start. If you are a pro, this is still the right place to start as we have tried to follow some best practices for Magento development that you might not be aware of. Let's get our hands dirty with Magento Development! Good luck!

Preparing the platform with a virtual host

Magento is built on the de facto PHP framework—Zend Framework. We need to set up our development environment properly to get most out of it. In this recipe, we will set up a Fully Qualifed Domain Name (FQDN) and a virtual host. We could use the domain as http://localhost/magento or something like this, but in that case accessing the admin panel might be cumbersome. In most cases, you have to access through the local IP address. Using a FQDN, you don't have to worry about it. A FQDN will make the debugging process much easier also.

Getting Ready

We need to have the following things installed before kicking off the setup of a virtual host a.k.a. vhost:

  • Apache2
  • PHP5
  • MySQL server 5.0

If we want to install the previously mentioned tools from Ubuntu Linux CLI, we can easily do that by running the following commands. We will be using Linux commands based on Ubuntu.

The following command is a basic Apache installation command:

sudo aptitude install apache2 apache2.2-common apache2-mpm-prefork 
apache2-utils libexpat1 ssl-cert

The following command is a basic PHP5 installation command:

sudo aptitude install libapache2-mod-php5 php5 php5-common php5-curl 
php5-dev php5-gd php5-imagick php5-mcrypt php5-memcache php5-mhash php5-
mysql php5-pspell php5-snmp php5-sqlite php5-xmlrpc php5-xsl

Enter the following command to begin with a simple MySQL installation:

sudo aptitude install mysql-server mysql-client libmysqlclient15-dev

Note that we have installed the development libs and headers with the libmysqlclient15-dev package. You can leave that out but it has been found that they are useful in many situations.

Alternately, we can use an all-in-one package, such as the XAMPP, to get all the aforementioned tools in one click. The XAMPP package can be downloaded from

How to do it...

  1. To test the domain without creating a DNS zone and record(s) on some Internet nameserver(s), let's modify the /etc/hosts file on our local computer to include some entries mapping the, and so on to the local machine's public IP address (in most cases, Open the /etc/hosts file with any text editor or run the following command in terminal:
    sudo nano /etc/hosts
  2. Add the following line as a similar entry in /etc/hosts file:

    The location of the host file depends on the OS loaded. If it's a Windows OS it should be c:\windows\system32\drivers\etc\hosts.

  3. Now let's create the layout for our domain. Open terminal and change your location to the WEBROOT directory by running the following command:
    cd /var/www/

    If you are using XAMPP, it would be c:\xampp\htdocs.

  4. Now for our domain, we want to host/create a folder with a standard set of subfolders:
    mkdir -p{public,private,log,cgi-bin,backup}
  5. Let's create an index.html file for our domain:
    sudo nano /var/www/
  6. It's time to put some content in our index file:
    <title>Welcome to</title>
    <h1>Welcome to</h1>
  7. We've set up the basics and now we're ready to add our own virtual hosts, so that we can start to serve our domain. Let's go ahead and create the vhost file for
    sudo nano /etc/apache2/sites-available/
  8. The contents should look like this:
    # Place any notes or comments you have here
    # It will make any customization easier to understand in the weeks
    to come
    # domain:
    # public: /var/www/
    <VirtualHost *:80>
    # Admin email, Server Name (domain name) and any aliases
    # Index file and Document Root (where the public files are
    DirectoryIndex index.php index.html
    DocumentRoot /var/www/
    # Custom log file locations
    LogLevel warn
    ErrorLog /var/www/
    CustomLog /var/www/ combined
  9. Now we have the site available, we need to enable it:
    sudo a2ensite
  10. It seems like good advice to reload the apache:
    sudo /etc/init.d/apache2 reload
  11. With such changes made, we can now navigate to our site in a web browser on our local computer, as seen in the following screenshot:

Tada! We now have the contents of public/index.html being shown.

How it works...

The Getting ready recipe describes the installation process of Apache, PHP, and MySQL from Linux command line. If you have already installed those, you can skip it and start confguring the layout and vhost directly.

The Magento source files will be put in the public directory. Other directories represent as their name suggest. Usually in production server, we don't have to write the domain name manually as we wrote here, as the DNS zone is there. The rest of this recipe is the same as the production server. The virtual host contents has some inline comments starting with # to describe the purpose.

Setting up a Subversion/SVN

A Subversion is a popular version control system initiated in 2000 by CollabNet Inc. Its goal is to be a mostly-compatible successor to the widely used Concurrent Versions System (CVS).

Getting Ready

If you are a Windows user, please download the TortoiseSVN client from here: and if you are a Linux user, fre up your terminal.

How to do it...

  1. Execute the following command in the terminal to install Subversion:
    sudo apt-get install subversion

    Make sure you have an active Internet connection as aptitude will install the package from Ubuntu repository.

    If you are using windows and intended to use TortoiseSVN you may get a Windows binary from

  2. After downloading the TortoiseSVN installer file, double-click on the TortoiseSVN installer file and follow the on screen instructions.
  3. Now let's add a new group called Subversion to our system and add current username to Subversion group.
    sudo addgroup subversion
    sudo adduser <your_username_here> subversion

    If you don't a have a clue about your current username, issue the command whoami in your terminal.

  4. Once the Subversion installation is completed, let's create our repository for Magento project. Now issue the following command in the terminal:
    sudo mkdir /home/svn
    cd /home/svn
    sudo mkdir magento
    sudo chown -R www-data:subversion magento
    sudo chmod -R g+rws magento
  5. Now initiate an SVN repository with the following command:
    sudo svnadmin create /home/svn/magento
  6. In the case of TortoiseSVN, open the folder where you want to put your Magento repository and right-click and find create repository here... from the context menu TortoiseSVN. Enter a name for your repository, in this case the repository name would be magento.
  7. It's time to change your current working directory in terminal to:
    cd /var/www/
  8. We will checkout the Magento project by issuing the following command in the terminal:
    svn co file:///home/svn/magento public

How it works...

When we execute any apt-get command, the aptitude will take care of the installation process. The aptitude will look for the package from Ubuntu central repository—and download it if necessary—and perform the necessary tasks.

After completion of the installation process, we've created a new group named subversion and added the username to the newly created group subversion. Then we've created a directory under /home/location to hold all repositories. After that, we've created a new repository for our Magento project named magento. Finally, we've checked out the repository in our site location in the public directory.

        Read more about this book      

Getting the latest copy of Magento with the SVN checkout

We have already installed Subversion and prepared the environment for firing up our Magento project. Let's grab the latest and greatest version of Magento 1.4 through SVN.

Getting ready

You need to have an active Internet connection to check out Magento from its repository and about 250 MB free space on your hard drive. Fire up your terminal and be ready to issue some necessary commands to grab Magento files in your box.

To create a directory for holding up all the SVN, check out the files and create a lib directory under /var/location as follows:

sudo mkdir /var/lib

How to do it...

We will now get the Magento 1.4 package from its repository and insert it into our local Magento repository.

Checking out Magento:

  1. Change your current directory to /var/lib/ by issuing the following command on your terminal:
    cd /var/lib
  2. Magento source is available at Issue the following command to check out Magento 1.4 version name as Magento-1.4:
    svn checkout
    trunk magento-1.4

Exporting checked out files:

  1. Let's export the checked out files to our local Magento project's public directory. Issue the following command to export the checked out files to your local Magento project:
    svn --force export /var/lib/magento-1.4/ /var/www/magento.local.

    We forced the export process as we already have a directory there, named public.

  2. Let's add it to our local Magento repository's working copy. Execute the following command to add the exported files to the Magento's working copy:
    cd /var/www/
    svn add *

    svn add will add all files recursively.

  3. Now commit the changes to local Magento repository:
    svn ci -m 'Magento-1.4 files added'

    Don't be upset if this takes a bit more time than you expected. There are too many files there in Magento you know!

How it works...

We've created a library for keeping Subversion checkout files for Magento in /var/directory and will grab the files through SVN checkout command from terminal. Then we exported the content to our Magento repository's working copy. Finally, we added and committed it to the local repository. We didn't SVN merge or import as Magento doesn't support it.

There's more...

You may read about other SVN commands from

Setting up MySQL tools

We will use MySQL Query Browser for MySQL GUI tool as Magento uses MySQL database. It's the official tool for handling MySQL databases. Let's get it.

Getting ready

Get your MySQL Query Browser installer from

How to do it...

  1. Run the installer that you downloaded from and follow the onscreen instructions.
  2. If you want to install it from Ubuntu terminal, fre up the terminal and execute this command:
    sudo apt-get install mysql-query-browser
  3. Now, we will create a database for our Magento project with MySQL Query Browser. Fire up MySQL Query Browser and create a new database named magento. Alternately, you can run this command in your MySQL terminal:

How it works...

Let's assume that you already know the basics of MySQL and any generic installation process. We simply installed the GUI tools to handle our MySQL database here and created a database named magento.

Setting up a Magento project with NetBeans

NetBeans is a powerful free and Open Source IDE, which you can use for almost any language. You can have a look why should we use it at

Getting ready

Get your copy of NetBeans from and install it. Make sure that you have installed the right download bundle, which is either PHP or All.

How to do it...

Now you should have everything ready and set up to create a Magento project in NetBeans.

  1. Start NetBeans. When the IDE comes up, create a new PHP project (File | New Project | PHP | PHP Application with Existing Sources; PHP Application from Remote Server). Note that we chose the second one, that is, PHP Application with Existing Sources, as we have the Magento source code already at hand.
  2. Then click Next.

  3. In the Sources Folder field, enter or select the directory where you previously exported the Magento sources (/var/www/
  4. Next, choose the Project Name (magento).
  5. Then click Next. The Run Confguration dialog will appear.

  6. In the Run Confguration step, select Local Web Site(running on local web server) in the Run As field.
  7. Enter the URL to open the main Magento page in the browser (
  8. Click the Finish button now to complete the project setup. Now you should have got something like this:

    (Move the mouse over the image to enlarge.)

How it works...

With the NetBeans IDE for PHP, you get the best of both worlds: the productivity of an IDE (code completion, real-time error checking, debugging, and more) with the speed and simplicity of your favorite text editor in a less than 30 MB download.

Upon completion of project setup, NetBeans will open Magento project files and scan the files for debugging information and Subversion history. This could take some time.

Working with Magento code

Somebody might tell you that Magento has too many files to deal with and its naming convention is crazy! Yes it is!!

But together they have a nice harmony. If you come to know the glitches to deal with this HUGE file structure, I bet you will love it. So, what are the glitches?

At first, let's know the file structure of Magento.

Getting ready

Start NetBeans and press Ctr + 1 or select Window | Project from the menu. Look at the file structure. We kept all Magento source files under the public directory. There are some other directories to help keep those necessary files.

How to do it...

  1. Our Magento project paths:
    / -private: Keeps the private files.
    / -cgi-bin: Here should be some cgi binaries if required.
    / -backup: Keep backup data.
    / -public: This is the Directory Root of Magento in our project.
    /app This is where all the PHP code for the application
    /lib - The external code library.
    /skin - This is where css and other images resides.
  2. A quick overview of app directory:
    /code - Where all types of modules resides.
    /design - Contains both layout xml files and template
    .phtml files.
    /etc - Global configuration.
    Mage.php - Static class Mage, where everything is held
  3. Before moving on to the other directories, let's see the directories in this (/app/code) dir:
    /community - Downloaded modules built by Magento community.
    /core - The core modules 'out of the box' build
    by Magento.
    /local - Any module we build ourselves.
  4. What about the /app/design directory?
    /adminhtml - Admin designs.
    /frontend - Front End designs.
    /default - This is the default Interface.
    /default – This is the default theme.
    /layout – Contains all the layout xml
    /template - Contains all the template
    .phtml files.
  5. Lastly, let's overview the /public/skin directory:
    /adminhtml - Admin styles directory.
    /frontend - frontend styles directory.
    /default - Our default interface.
    /default - Our default theme.

How it works...

Magento class names follow a strict convention, which is Camel case and separated by underscores. You can easily interpret any class name to its location by replacing the underscores to forward slashes.

We are not compelled to follow Magento conventions, but if we follow Magento conventions, we will get some extra benefts such as—Magento will take the name, replace underscores with forward slashes, include that file, then load the requested class, and return an instance of that class. Pretty cool!

There's more...

If you want to download the sample data in another format, visit this page and download your own:

Make sure you have run the SQL and pasted the media directory before starting the Magento installation.

You can now point your browser to kick off the Magento installation process. If this is your first time in Magento, you should also install the sample data provided by Magento. You may download the sample data for Magento 1.4 version from here:


This article introduced us to Magento and described how to set up different tools required for a Magento project. It also illustrated how to work with Magento code.

You've been reading an excerpt of:

Magento 1.4 Development Cookbook

Explore Title