Magento Development

From Crowfly

Jump to: navigation, search

The following are summary notes of Magento for Developers: Part 1—Introduction to Magento by Alan Storm, updated for Magento 1.12


Part 1

Code Area

Magento is MVC. The code contains Controllers, Models, Helpers, Blocks, and Routers


Package / Modules

Package is like a namespace for your company


Add modules to system


Modules config.xml becomes part of systems large config tree


On does not edit index.php

index.php does the following

  1. Parse URL
  2. Routes - maps package/module/action to Controller method
  3. Dispatches


Zend Framework Like


module: catalog
front-controller: category
action: view
params: id=15

Routing methods:

  1. Routes in Config
  2. Routes in Admin (?)
  3. Mage_Cms_IndexController



All controllers extend Mage_Core_Controller_Front_Action Class. Note the term 'core' in that controller means there are two cores in the complete path: /app/code/core/Mage/Core/...

class Mage_Catalog_CategoryController extends Mage_Core_Controller_Front_Action
    public function viewAction()
        //main entry point

Add controllers to your module's config.xml


Context-Based URI Model Loading


Grouped Class Name, Aka. URI: 'catalog/product' Module: catalog Class: product



$model = Mage::getModel('catalog/product')->load(27);
$price = $model->getPrice();
$price += 5;


$products_collection = Mage::getModel('catalog/product')

foreach($products_collection as $product)
    echo $product->getName();

addAttributeToSelect brings in EAV values.

Using Varien_Object's __call method:

$product->getPrice() and -->setPrice() methods gets and sets product.price column data.

$product->getData() returns array of all attributres.


$helper = Mage::helper('catalog');  # same as ...
$helper = Mage::helper('catalog/data');

Most Helpers inherit form Mage_Core_Helper_Abstract, which gives you several useful methods by default.

$translated_output =  $helper->__('Magento is Great'); //gettext style translations
if($helper->isModuleOutputEnabled()): //is output for this module on or off?


The "V" in MVC

Layout = nested/tree collections of "Block" objects. Block objects render bits fo HTML. They are *.phtml files. Block objects call Model methods.

Controller has to call


Think of Blocks as either Structure (ie. Head, body, multiple columns layout, etc), or as Content.

You can add blocks programmatically

public function indexAction()
    $block = $this->getLayout()->createBlock('adminhtml/system_account_edit')

But more commonly, in layout.xml

    <reference name="left">
        <block type="catalog/navigation" name="catalog.leftnav" after="currency" template="catalog/navigation/left.phtml"/>

Module: catalog
Controller: category
Action: default

Nested Blocks

    <reference name="left">
        <block type="catalog/navigation" name="catalog.leftnav" after="currency" template="catalog/navigation/left.phtml">
            <block type="core/template" name="foobar" template="foo/baz/bar.phtml"/>

You'd be able to call $this->getchildHtml('foobar'); from the Block object.


Observers listen to events

class Packagename_Mymodule_Model_Observer
    public function iSpyWithMyLittleEye($observer)
        $data = $observer->getData();
        //code to check observer data for our user,
        //and take some action goes here

Class Overrides

Monkey Patching Core

Clone and add functionality to Mage/Catalog Product Model. One does not clone an entire module, only individual classes, as needed.

class Packagename_Modulename_Model_Foobazproduct extends Mage_Catalog_Model_Product
    public function validate()
        //add custom validation functionality here
        return $this;


In the config.xml:

    <!-- does the override for catalog/product-->

Part 2


Config describes modules, models, classes, templates, etc.

Claims to provide great flexibility - however I find it way overly abstracted and unnecessary. Compare it to Rails, or Django

New Module


Package: Tommiecopper
Module: Configviewer

Need to add these two files, too:




Clear cache

System > Configuration > Advanced > Disable modules output: verify you see the new module.

Create Module Config



Part 3

Magento Controllers

Part 4

According to : Divante

The view layer in magento itself implements the MVC pattern

M - Blocks    - /app/code/local/Tommiecopper/Configviewer/Block/product.php
V - Templates - /app/design/frontent/enterprise/tctwo/templates/product.phtml
C - Layouts   - /app/design/frontent/enterprise/tctwo/layouts/product.xml

According to Alan Storm:

Layouts, Blocks, and Templates

Magento View components = Blocks and Templates

Controller does NOT pass variables to the View - The View components call module methods directly.

Blocks = PHP objects
Templates = HTML + PHP

$this refers to the Template's Block object.

Nested Blocks

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="" xml:lang="<?php echo $this->getLang() ?>" lang="<?php echo $this->getLang() ?>">
<?php echo $this->getChildHtml('head') ?>
<body class="page-popup <?php echo $this->getBodyClass()?$this->getBodyClass():'' ?>">
    <?php echo $this->getChildHtml('content') ?>
    <?php echo $this->getChildHtml('before_body_end') ?>
    <?php echo $this->getAbsoluteFooter() ?>


<layout version="0.1.0">
        <block type="page/html" name="root" output="toHtml" template="magentotutorial/helloworld/simple_page.phtml" />

Personal tools