This entry was posted on Tuesday, March 31st, 2009 at 4:07 am and is filed under development, howto. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Mar 31, 2009
Howto: Repackageable custom extension development in Magento
Author: gaweee | Filed under: development, howto
Magento code is awesome. So awesome that 20 wiki entries and a 100 files open later i’m was still lost. So much that i ordered the PHP|architects Magento book! At first i was disappointed in its coverage but it did help me out along the way. So heres a synthesized description of our efforts. We’re breaking up this HOWTO into a few segments into increasing orders of complexity and integration.
Note: Apparently i’ve been giving our dear readers the misconception that the book is what taught me all these techniques. WRONG. The book’s lack of coverage is what prompted me to write this. I’m not sure i was any better off reading the book!
If you have a really good idea of what you’re doing/not doing, you could cut right through the chase and download the entire tutorial’s code here: Magento Tutorial …
Otherwise, let us begin …
Do us all a favour and get your cache management right (disabled) from the beginning
Go to your system backend: http://127.0.0.1/magento/index.php/admin
Login and click ok System > Cache Management
Then on the All Cache dropdown menu, select Disable
Save cache settings
Create barebones code, file structure and config
We’re trying to achieve a proof of concept here to describe how the file structure, namespaces and config files interplay to achieve a Hello world message
- Create your directory structure! Our NEW company name is Savant Degrees! and our module shall be called….. Twit (for a lack of an imagination). The whole module (well, almost) should rest in the
/app/code/local>folder. So lets first create the following folder structure. For a hello world module, you only need aconfigfile, acontrollerand ablock. Ergo…app/ etc/ modules/ - SavantDegrees_All.xml (Or what ever your company name might be) code/ local/ SavantDegrees/ (Or what ever your company name might be) Twit/ (Or whatever your module name might be) Block/ - Index.php controllers/ - IndexController.php etc/ - config.xml - Create your
config.xmlfile. Thats the crux of the whole module.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<?xml version="1.0"?> <config> <modules> <SavantDegrees_Twit> <version>0.1.0</version> </SavantDegrees_Twit> </modules> <global> <blocks> <twit> <class>SavantDegrees_Twit_Block</class> </twit> </blocks> </global> <frontend> <routers> <SavantDegrees_Twit> <use>standard</use> <args> <module>SavantDegrees_Twit</module> <frontName>twit</frontName> </args> </SavantDegrees_Twit> </routers> </frontend> </config>
Let me explain this step a little…
The
modules > [module] > versionnumber allows Magento to track when your module is upgraded. We’ll be seeing a version of that shortly.
Theglobal > blocks > [module]define the Block resources that are available under your package. We’ll be using this to generate a Hello world shortly
Thefrontend > routersfragment define how the module can be access. The current code allows the block to be access via https://127.0.0.1/magento/index.php/twit - Create your
IndexController.phpfile. Its as simple as:1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php class SavantDegrees_Twit_IndexController extends Mage_Core_Controller_Front_Action { public function indexAction() { $this->loadLayout(); $this->getLayout() ->getBlock('content')->append( $this->getLayout()->createBlock('twit/index') ); $this->renderLayout(); } }
More Explanation: The
twit/indexrefers to the index block inside the Twit module, which is defined in the config file above. We’re trying to put its contents inside the layout. - Lastly, we’ll create the
Index.phpBlock:1 2 3 4 5 6 7 8
<?php class SavantDegrees_Twit_Block_Index extends Mage_Core_Block_Template { protected function _toHtml() { return 'Hello world'; } }
You DO understand SOME PHP don’t you?
- Finally, we activate the module by creating the file
SavantDegrees_All.xmlfile1 2 3 4 5 6 7 8 9
<?xml version="1.0"?> <config> <modules> <SavantDegrees_Twit> <active>true</active> <codePool>local</codePool> </SavantDegrees_Twit> </modules> </config>
- Wake up the Magento installer by logging into the
adminbackend,System > Configuration > Advanced. Click onSave Config. Clear your cache at/var/cachetoo just to be sure.
Tada! Navigate to https://127.0.0.1/magento/index.php/twit to see your well deserved Hello World!
Next … Part 2 – Admin Controller
8 Responses to “Howto: Repackageable custom extension development in Magento”
Leave a Reply
Most Popular
- HOWTO: PHP and jQuery upload progress bar (47)
- JQuery Progress Bar 1.1 (41)
- Howto: Repackageable custom extension development in Magento - Part 2 - Admin Controller (24)
- Howto: Repackageable custom extension development in Magento - Part 8 - CRUD - Update (13)
- HOWTO: struts 2 i18n (12)
- JQuery Progress Bar 2.0 (11)
- JQuery Progress Bar 1.2 (10)
- Howto: Repackageable custom extension development in Magento - Part 3 - Database (9)
- Howto: Repackageable custom extension development in Magento (8)
- Howto: Repackageable custom extension development in Magento - Part 5 - CRUD - Retrieve (6)
Recent Comments
- donald: Hi Wen, I have checked out
- donald: Hi Ashley, Currently, I simply use
- donald: Hi Noemi, there are many
- Serge: Thanks a lot. It's the simplest
- Zoran: Excellent posts you got here...
- Craig: Brilliant tutorial! There's NOTHING on
- Will: Thnak you! I was
- Margots: Thank you a lot for
- Aswin S: Wow great dude.. For the
- Noemi Heier: This is great! How did
Latest Entries
- jQuery Progress Bar Configuration
- Extracting email addresses from inbox
- 10 Good (Free and Legal) Source for Photos and Images
- Howto: Backup Microsoft SQL Server Database, as in Dump it to a SQL Script (like MYSQL's sqldump)
- Managing client's expectation with wireframe software
- Howto: Repackageable custom extension development in Magento - Part 9 - Frontend - List
- JQuery Progress Bar 2.0
- HOWTO: Find icons for your new prototype system
- Google Maps Helper
- laying the cornerstones
January 25th, 2010 at 11:08 pm
Thanks a lot.
It’s the simplest way to start programming magento among those I’ve found in www
September 15th, 2009 at 11:03 am
go ahead man
thanks
i get the prob the problem was with th digram
September 15th, 2009 at 10:58 am
hello
thanks for your helpful . but i did what you explain >> i don’t know why i get ERROR 404
i tried to do it again >>> but the same prob i met
could you get me the reason if u can
May 8th, 2009 at 9:46 am
This is a very helpful guide. Thank you!
In my case I was getting a Magento error ‘Controller file was loaded but class does not exist’ until I noticed there was no opening ‘<?php’ in the IndexController.php file. I added that, voila! Progress.
April 12th, 2009 at 7:10 am
Thanks! I’ve updated the code to reflect those changes!
April 12th, 2009 at 6:49 am
o0o I worked it out … in your directory structure diagram you have indexController.php however later on in your code you have IndexController.php
notice the capital I… the I needs to be capital.. mine was lower case thanks again
err ps.. I posted this follow up on part 4 two by mistakes :$
April 12th, 2009 at 6:45 am
Hi there,
Thanks for a great tutorial, I’am having some troubles though…
I followed the tutorial on this page step by step for hello world… however changed the company and module name.. the new module shows up in config>advanced and is enabled, I then clear cache and tried to access it from demoshop/mymodname and I get the magento 404 message..
So I start over, not changing any names or anything.. keep the code exactly as you have it above… and it again appears in config as enabled, so I reset cache and try to access demoshop/twit and agian get the magento 404…
any assistance on this would be much thought of
many thanks,
Jim
April 1st, 2009 at 5:58 am
Thank you – you tutorial is the first one I found where everything just works and has a good explanation as well. Thanks again.
Claudia