An Introduction to Magento E-Commerce
Since I have been working with the Magento E-Commerce software as of late, I will be publishing a series of blog posts to share what I have been learning with everyone. For those of you who don’t know, Magento is a popular PHP/MySQL e-commerce platform that is super-powerful and infinitely customizable. Writing software and customizations for Magento is notorious for its steep learning curve. It has foundations in the Zend framework, so if you are familiar with Zend, then theoretically you have a head-start on that curve.
Magento Basics
If you are just starting out be forewarned, Magento has a STEEP learning curve, but once you get the hang of it, things start to smooth out and fall into place.
Model-View-Controller
Magento is a Model-View-Controller (MVC) framework that adheres to the motto “configuration over convention”. This simply means that, unlike other MVC frameworks (like CakePHP or Ruby on Rails), in most cases you are specifically telling Magento where to look for MVC components. It doesn’t do much automatically other than auto-loading classes.
The major components of Magento’s architecture are:
- Model – Responsible for containing the business logic of the application. Talks to the datastore (MySQL).
- Block – A component of the view layer in MVC. Represents contained portions of the view that can be reused throughout a page.
- Layout – A component of the view layer in MVC. Determines which components (blocks/templates) get rendered on a page.
- Template – A component of the view layer. Renders the actual html that will be displayed in the response.
- Controller – Contains “actions” which correspond to URL requests. Loads the layout for said action and any models needed for business logic, then renders the output of the page.
As you can see, the Magento framework splits the view into three separate parts: blocks, layouts, and templates. This can add confusion to the learning process, but in the end it results in more organized and object-oriented code.
Directory Structure
The Magento directory structure is very deep. It can seem like a maze to newcomers, and even seasoned Magento programmers can find it clunky at times. When you are doing some hardcore Magento programming, it always helps to use an IDE like Eclipse, which will allow you to hyperlink to various classes and functions within the code-base. Otherwise, grep is your best (and perhaps only) friend!
To keep things brief, I am just going to go over a few key directories.
/app/code
This is where the models, blocks and controllers reside. They are organized in segments called modules, which are of three classes: core, local, and community. Core modules are what gets shipped with the system. Local modules are custom modules that you install manually. Community modules are installed from the Magento Connect repositories via PEAR.
/app/code/local
This is where all of your local modules reside To keep things organized, developers are expected to contain all of their modules within a “unique” namespace of their choosing. For example, if I chose the namespace “FranklinStrube”, then each of my modules would be its own sub-directory in /app/code/local/FranklinStrube
.
/app/design
This is where your layout and template files are stored. The sub-directory structure separates the frontend from the administrative section (adminhtml). The sub-directory structure from there contains theme-specific files. For example, the initial frontend theme is “default”, so Magento loads the view files from the appropriate folder under /app/design/frontend/default/default. If the view file cannot be found their, then it loads the view files from the appropriate folder under /app/design/frontend/base/default. So, if you changed the theme to “coolred”, then it would first look for the appropriate file under /app/design/frontend/default/coolred first, then fallback to /app/design/frontend/base/default.
/app/etc
This folder holds your global configuration file (local.xml), which is where you tell Magento how to connect to your database. There is also a folder called modules, which is where you enable any custom modules for your namespace.
/lib
The lib folder contains all of the core libraries, like the Zend Framework, and any of Varien’s core files.
Next Steps
This article barely scratched the surface of Magento. If you are serious about developing for the Magento platform, I would recommend reading through the tutorials in the Magento Knowledge Base. You can also find good tutorials and examples on the following sites:
- http://alanstorm.com/
- http://blog.baobaz.com
- http://www.edmondscommerce.co.uk/category/magento/
- http://colin.mollenhour.com/
Suggested Reading
Magento: Beginner’s Guide