Magento (magentocommerce.com) is a powerful e-commerce solution written in PHP that is offered as Commercial Open-Source. It has quickly surpassed other PHP solutions (Zencart,etc) to become a quality product for the amateur and for a budding enterprise.
There are several challenges, though, when you try and develop in Magento. Documentation is somewhat sparse and there are a multitude of assumptions in naming conventions that hold the fate of your application in its invisible hands. So I’ve created this blog entry to demonstrate how to create a custom administration module–a screen that can be used by your site administrators with custom functionality.
I’m going to create an app called “Foo”
Step 1 – Register your new module
First, locate the following folder inside your Magento installation:
app/etc/modules
This is where Magento learns about Modules that have been installed. Create a file for your module named RobbyMillsap_Foo.xml. The contents of my file look like this:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<RobbyMillsap_Foo>
<active>true</active>
<codePool>local</codePool>
</RobbyMillsap_Foo>
</modules>
</config>
Here’s the thing…
Magento has a few naming conventions you’re going to have to become intimately familiar with. Where you store your code and design files will be dependent on how you named this module. In our example above, the namespace we are using is called RobbyMillsap and the module is called Foo.
Step 2 – Create an Admin controller
I’m assuming you are familiar with how an Model-View-Controller (MVC) architecture works. Here is the concept in one line: the Model is the representation of your data, the View is responsible for rendering the screen and the Controller is responsible for handling user action and communicating with the services (DB, web services, etc).
Our controller is going to be located in this folder:
app/code/local/RobbyMillsap/AdminTab/controllers/FooController.php
This is a perfect example of what I was saying about Magento’s tricky naming assumptions. How would else would you know that this is the exact place for your admin controller to be created unless I told you? Well, they have some tutorials, granted. But it would make more sense, in my opinion, if they let you register this WITH the module definition…but back to our project. Let’s create the controller class. Here is mine:
class RobbyMillsap_AdminTab_FooController extends Mage_Adminhtml_Controller_Action{
public function indexAction(){
}
}
Ok, so again with the naming conventions. You have to use <namespace>_AdminTab_<module> as the name of your class again here or else it will not be picked up by Magento’s interpreter.
Step 3 – Create a simple view
A file with a .phtml extensions can serve both plain HTML and PHP code. This is the convention used for Magento template files. For our view, let’s create a template file in the following location
app/design/adminhtml/default/default/template/foo/index.html
Again, so many assumptions made here by Magento. But oh well; it doesn’t matter what you put in this file, perhaps just the phrase “Hello world” for now. It will be rendered inside Magento template when are done.
Step 4 – Load your view in the controller
Now that the controller has been created you can create a simple template and have it rendered on the screen. Here’s the code to put inside your indexAction() function:
$block = $this->getLayout()->createBlock('Mage_Core_Block_Template','foo', array('template' => 'foo/index.phtml') );
$this->getLayout()->getBlock('content')->append($block);
$this->renderLayout();
This code loads a view object and saves it as block. We can assign objects to be passed to the view later (like a collection of database rows).
That’s all for now
Ok folks, that’s all for now. As you can see, Magento has A LOT of moving pieces, a lot of configuration files, and a lot of unclear naming assumptions. It’s still a powerful e-commerce tool, but not for the faint of heart.