Home Web-development Magento 1.4 Development Cookbook

Magento 1.4 Development Cookbook

By Nurul Ferdous
books-svg-icon Book
Subscription
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
Subscription
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Getting Started with Magento Development
About this book

Magento is the fastest growing PHP-based e-commerce solution based on the Zend Framework. It is a robust CMS that provides developers the ability to enrich their store with extra functionalities using custom modules. Developing a Magento store to get the desired look and feel is not as easy as you might believe and may take hours due to the wealth of features available for you to use.

Magento 1.4 Development Cookbook provides unparalleled guidelines to develop a much faster and more captivating Magento store by writing powerful custom modules. The book covers everything from common development tasks to integrating social networking plugins into Magento.

If you choose to work through all the recipes from the beginning, you will have a development platform ready to work with Magento. You will also explore different ways to customize the look and feel of a Magento store to offer your customers a better user experience. Integration of Magento with WordPress to add striking functionality to your store will be accomplished in just a few steps. Some cool recipes take care of the Magento security measures by editing the parameters in the admin panel. Setting up a master-slave setup for Magento database is discussed along with other database optimizations in the database chapter. Developing new modules and widgets for Magento is thoroughly described. Optimizing Magento's performance is the most important part of this book, which is armed with some easy and incredible recipes dealing with YSlow, Page Speed, Siege, Apache bench, Apache configuration, php.ini optimization, and caching with APC or Memcached. The work procedure behind the wall is explained in an easy manner so that both novice and experienced developers can benefit from it. This book also has some recipes that are not only useful for Magento but also for any other LAMP-based project.

Publication date:
December 2010
Publisher
Packt
Pages
268
ISBN
9781849511445

 

Chapter 1. Getting Started with Magento Development

In this chapter, we will cover:

  • Preparing the platform with a virtual host

  • Setting up a Subversion/SVN

  • Getting the latest copy of Magento with the SVN checkout

  • Setting up MySQL tools

  • Setting up a Magento project with NetBeans

  • Working with Magento code

 

Introduction


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 chapter. 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 Qualified 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 throughout the book.

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

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 http://www.apachefriends.org/en/xampp.html.

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 magento.local.com, and so on to the local machine's public IP address (in most cases, 127.0.0.1). 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:

    127.0.0.1       magento.local.com
    

    Note

    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 magento.local.com/{public,private,log,cgi-bin,backup}
  5. Let's create an index.html file for our domain:

    sudo nano /var/www/magento.local.com/public/index.html
  6. It's time to put some content in our index file:

    <html>
      <head>
        <title>Welcome to magento.local.com</title>
      </head>
      <body>
        <h1>Welcome to magento.local.com</h1>
      </body>
    </html>
  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 magento.local.com:

    sudo nano /etc/apache2/sites-available/magento.local.com
  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: magento.local.com
    # public: /var/www/magento.local.com/public
    <VirtualHost *:80>
      # Admin email, Server Name (domain name) and any aliases
      ServerAdmin webmaster@magento.local.com
      ServerName  magento.local.com
      # Index file and Document Root (where the public files are located)
      DirectoryIndex index.php index.html
      DocumentRoot /var/www/magento.local.com/public
      # Custom log file locations
      LogLevel warn
      ErrorLog  /var/www/magento.local.com/log/error.log
      CustomLog /var/www/magento.local.com/log/access.log combined
    </VirtualHost>
  9. Now we have the site available, we need to enable it:

    sudo a2ensite magento.local.com
  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 configuring 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. We will use this tool throughout the book for source controlling. 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: http://tortoisesvn.tigris.org/ and if you are a Linux user, fire up your terminal.

How to do it...

  1. Execute the following command in the terminal to install Subversion:

    sudo apt-get install subversion

    Note

    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 http://tortoisesvn.tigris.org.

  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

    Note

    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/magento.local.com
  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.

 

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 http://svn.magentocommerce.com/source/branches/1.4-trunk. Issue the following command to check out Magento 1.4 version name as Magento-1.4:

    svn checkout http://svn.magentocommerce.com/source/branches/1.4-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.com/public/
    
    

    Note

    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/magento.local.com/public
    svn add *

    Note

    svn add will add all files recursively.

  3. Now commit the changes to local Magento repository:

    svn ci -m 'Magento-1.4 files added'

Note

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 http://svnbook.red-bean.com/.

 

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 http://dev.mysql.com/downloads/gui-tools/5.0.html.

How to do it...

  1. Run the installer that you downloaded from http://dev.mysql.com/downloads/gui-tools/5.0.html and follow the onscreen instructions.

  2. If you want to install it from Ubuntu terminal, fire 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:

    CREATE DATABASE `magento` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

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. We will use this IDE throughout this book. You can have a look why should we use it at http://netbeans.org/switch/why.html.

Getting ready

Get your copy of NetBeans from http://netbeans.org/downloads/index.html 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/magento.local.com).

  4. Next, choose the Project Name (magento).

  5. Then click Next. The Run Configuration dialog will appear.

  6. In the Run Configuration 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 (http://magento.local.com/).

  8. Click the Finish button now to complete the project setup. Now you should have got something like this:

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...

  • 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 resides.
        /lib      - The external code library.
        /skin     - This is where css and other images resides.
  • A quick overview of app directory:

    /app
        /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 together.
  • Before moving on to the other directories, let's see the directories in this (/app/code) dir:

    /code
        /community - Downloaded modules built by Magento community.
        /core            - The core modules 'out of the box' buildby Magento.
        /local            - Any module we build ourselves.
  • What about the /app/design directory?

    /app
        /design
          /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 files.
                /template     - Contains all the template .phtml files.
  • Lastly, let's overview the /public/skin directory:

      /public
        /skin
          /adminhtml   - Admin styles directory.
          /frontend    - frontend styles directory.        /default - Our default interface.
              /default    - Our default theme.
                /css    
                /images
                /js

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 benefits 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:

http://www.magentocommerce.com/knowledge-base/entry/installing-sample-data-archive-for-magento-120.

Note

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

Some notes on installation and sample data

You can now point your browser http://magento.local.com 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: http://www.magentocommerce.com/downloads/assets/1.2.0/magento-sample-data-1.2.0.tar.gz.

About the Author
  • Nurul Ferdous

    Nurul Ferdous is an open source enthusiast and IT specialist from Bangladesh who is currently working for TM Secure Inc. as a LAMP consultant. In fact, he is a soldier turned programmer. He started his career with the Bangladesh Air Force. He has also served in RAB as an intelligence staff where he was nominated for the President's Police medal for his contribution to national security. He is a true passionate programmer. He started his run on software development back in 2004, while he was working in the Bangladesh Air Force. His primary skills are as a PHP developer. He is a Zend Certified PHP 5 Engineer, and contributes to a number of PHP projects, blogs on PHP-related topics, and presents talks and tutorials related to PHP development and the projects to which he contributes. He also contributes on open source community regularly. He is also a certified professional on TDD and Code Refactoring. He has served in some top notch software companies both at home and abroad, such as BIPL, Right Brain Solutions Ltd., TM Secure Inc., NameDepot.com Inc., and so on, as a programmer, software engineer, and consultant. He also writes at his personal blog http://dynamicguy.com when he is not baking with codes.

    Browse publications by this author
Magento 1.4 Development Cookbook
Unlock this book and the full library FREE for 7 days
Start now