Technology Powered Knowledge Base
Click Here To Make FREE Audio & Video Call From Web Browser!

Magento MVC Pattern and Advanced Module Development Guide

VN:F [1.9.22_1171]
Rating: +15 (from 15 votes)
VN:F [1.9.22_1171]
Rating: 10.0/10 (15 votes cast)

Part of customizing Magento is, of course creating custom modules. These allow you to inject functionality anywhere, whether in a “static” block fashion that’s more than static, or a payment module, or large module to do something as large as integrating a 3rd party system.
 
Magento module fully structured with MVC pattern and there are many things, from editing the database to handling module upgrades to overriding classes (Blocks, Controllers, Models) and more!
 
This is a very basic start on creating custom module and hooking it up to a phtml file in own theme.

We can consider this module as Fido. ‘Fido’ is the namespace (vs Mage) for Magento.
 
Step One
First need to inform Magento that we have custom module. Go to, app/etc/modules/Fido_All.xml Notice the _All in the xml file name. I can declare all of my modules here. (Say I have more than Example, I can also declare my Example2 module in this file).


<?xml version="1.0" ?>
<config>
    <modules>
        <Fido_Example>
            <active>true</active>
            <codePool>local</codePool>
        </Fido_Example>
    </modules>
</config>

I have informed Magento that I have an active module (you can turn it off from here by setting ‘active’ to false. I have also informed Magento that it is located in the ‘local’ code pool.
 
Step Two
Configure your new module. Note the file locations (need to create directories as necessary).
app/code/local/Fido/Example/etc/config.xml


<config>
    <modules>
    <fido_example>
        <version>0.1.0</version><br>
    </fido_example>
    </modules>
    
    <global>
    <blocks>
    	<fido_example>
    		<class>Fido_Example_Block</class>
    	</fido_example>
    </blocks>
    </global>
</config>

Now informed Magento about module version (it’s an arbitrary version). Version matters when you set up your module to be update-able. (A newer version will inform Magento to run the update files if you have them).
Magento also informed that module contains  block files which are found in fido/example/block. Module name will have “Fido_Example_Block”. If want to see the many possibilities of stuff that goes in here, check out Mage config files (such as Catalog/etc/config.xml). You’ll also see other xml files in there.
 
Step Three
Here is block code. It doesn’t really do anything, but shows some functionality. It’s app\code\local\Fido\Example\Block\View.php


/**
* Example View block
*
* @codepool   Local
* @category   Fido
* @package    Fido_Example
* @module     Example
*/
class Fido_Example_Block_View extends Mage_Core_Block_Template{
    private $message;
    private $att;
    protected function createMessage($msg) {
    	$this->message = $msg;
    }
    public function receiveMessage(){
        if($this->message != '') {
            return $this->message;
        }else{
        	$this->createMessage('Hello World');
        	return $this->message;
    	}
	}
    protected function _toHtml() {
        $html = parent::_toHtml();
        if($this->att = $this->getMyCustom() &amp;&amp; $this->getMyCustom() != '') {
            $html .= $this->att;
        }else{
            $html .= 'No Custom Attribute Found';
        }
        return $html;
   }
}

The function receiveMessage() just returns “Hello World”
The function _toHtml() is responsible for outputting the template associated with the block. Make sure you run paret::_toHtml and return it as part of any other items returned in that function!
 
Step Four

Here we create our template (phtml) file. app\design\frontend\default\fido\template\example\view.phtml


/**
* Fido view template
*
* @see Fido_Example_Block_View
*
*/

echo $this-&gt;receiveMessage();

This just outputs some HTML and also runs the receiveMessage() function from our block (view.php).
Two caveats here. By placing our view.phtml file in it’s location, we have created our own theme. You must make sure that
a) Magento knows about your theme (Admin->System->Design)
b) If you use the this block in a CMS page, you set the CMS page to use your theme (Admin->CMS->Manage Pages->’Your Page’->Custom Design->Custom Theme drop down)

Our custom module is now ready for use.In a cms page, add this to your content:


{{block type="fido_example/view" my_custom="Test" template="example/view.phtml" }}

Or something like this in the Layout Update XML area (Custom Design area in a CMS page)
 
Now we should successfully have the block and the Hello World message being displayed.

Magento MVC Pattern and Advanced Module Development Guide, 10.0 out of 10 based on 15 ratings



Sign Up     Sign In