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!
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.
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.
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 themagento.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
Add the following line as a similar entry in
/etc/hosts
file:127.0.0.1 magento.local.com
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
.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}
Let's create an
index.html
file for our domain:sudo nano /var/www/magento.local.com/public/index.html
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>
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
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>
Now we have the site available, we need to enable it:
sudo a2ensite magento.local.com
It seems like good advice to reload the apache:
sudo /etc/init.d/apache2 reload
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.
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.
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).
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.
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.
After downloading the TortoiseSVN installer file, double-click on the TortoiseSVN installer file and follow the on screen instructions.
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
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
Now initiate an SVN repository with the following command:
sudo svnadmin create /home/svn/magento
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
.It's time to change your current working directory in terminal to:
cd /var/www/magento.local.com
We will checkout the Magento project by issuing the following command in the terminal:
svn co file:///home/svn/magento public
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.
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.
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
We will now get the Magento 1.4 package from its repository and insert it into our local Magento repository.
Checking out Magento:
Change your current directory to
/var/lib/
by issuing the following command on your terminal:cd /var/lib
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:
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/
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 *
Now commit the changes to local Magento repository:
svn ci -m 'Magento-1.4 files added'
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.
You may read about other SVN commands from http://svnbook.red-bean.com/.
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.
Get your MySQL Query Browser installer from http://dev.mysql.com/downloads/gui-tools/5.0.html.
Run the installer that you downloaded from http://dev.mysql.com/downloads/gui-tools/5.0.html and follow the onscreen instructions.
If you want to install it from Ubuntu terminal, fire up the terminal and execute this command:
sudo apt-get install mysql-query-browser
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;
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.
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.
Now you should have everything ready and set up to create a Magento project in NetBeans.
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.
Then click Next.
In the Sources Folder field, enter or select the directory where you previously exported the Magento sources (
/var/www/magento.local.com
).Next, choose the Project Name (
magento
).Then click Next. The Run Configuration dialog will appear.
In the Run Configuration step, select Local Web Site(running on local web server) in the Run As field.
Enter the URL to open the main Magento page in the browser (
http://magento.local.com/
).Click the Finish button now to complete the project setup. Now you should have got something like this:
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.
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.
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.
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
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!
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.
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.