Thanks to member Danieln for creating this PHP script to automatically create a base module given a Namespace and Module.
Credits: Danieln (creator), Alistek (contributor), Damián Culotta (contributor), Somesid (contributor), Ahsan Shahzad (contributor)
Updated: 25/08/2008 - 0.0.9.1 Release Notes
#NOTE: v0.0.9.1 holds a bug that do not allow to ACL to manage module’s user level access. I am currently working on it to fix.
#ADDED: The module is now created with an upload field for example.
#ADDED: Code in the frontend controller shows how to get an object by param and by custom query .
#NOTE: Wiki post has yet to be updated with new code and explanations.
Updated: 07/04/2008 - 04:00AM CST - 0.0.9.0 Release Notes
#NEW FEATURE: Now with module uninstaller and better look&feel.
#NEW FEATURE: Point to your Magento Install Directory to get you module copied to the right places right away.
#NOTE: Thanks to Barbanet the below problem should be alleviated with entering in your interface and theme at module creation time.
#NOTE: A common problem with the Module Creator has been the confusion over the use of ‘interface’ and ‘theme’.
In a new Magento installation correspond to:
Please make sure to put your <module>.xml files in the correct ‘layout’ directories as these will determine whether your module is shown or not.
To install the Module Creator, you can use the Magento Connect Manager with the extension key, or install it manually as follows. Note that if you install via the Connect Manager the path it uses is /moduleCreator.
Copy the index.php file and the Blank folder to your webserver. You may want to put it in it’s own folder so that the index file does not conflict with other index files. Example: mysite.com/module_creator/
Go to the module creator index page in your web browser, and fill in the desired module information. If you enter your Magento directory, the module creator will attempt to install the module directly into Magento, though file permissions must be set correctly for this to happen. If your file permissions are incorrect, or you do not want the module installed right away, you can leave the Magento Directory blank, and the module creator will create the module in a folder called [i]new[/i], and place it in the module creator’s folder.
Please note that if you enter in your magento directory to install to you must have the correct path. Let’s say that you have Magento installed here:
And you have the Module Creator installed here:
This means that the path that you would need to enter in the Module Creator is this:
The ‘..’ signifies one directory up.
I am going to use a few common conventions here that should make it easier to figure out what you need to replace as I am making this as generic as possible.
Anything in angled brackets (including the angled brackets):
< >, needs to replaced with the appropriate text..
Anything in square brackets (including the square brackets):
[ ], needs to replaced with the appropriate text.
The reason I am using two different conventions here are that XML files already use angled brackets so you will only see the square brackets in use in XML files.
NOTE: All directory, file, class names are Case Sensitive unless otherwise noted.
Magento Modules follow a common naming scheme, Namespace_Module. This is very important to remember as you need to be careful about how you name your classes. Every custom module will be created in the directory:
The first step in creating your custom module is determining the namespace and module name. The namespace is simply an extra division that you can create you module in. This means that you can have two modules named the same thing if they are in two different namespaces. One way to use namespaces is to use your company name or initials for all of your modules. If my company name is Acme and I am creating a module called News the full module name would be Acme_News. Magento uses the namespace Mage. There is nothing stopping you from using that namespace under local, i.e. you could create Mage_News and that would work just fine.
Note : You can not use underscore within your module name
Note2: It seems that currently, if you use upper case characters in module names (expecting to show word starts since neither - nor _ is allowed)... the install will fail or rather the module will not work. Suggestion: use a single upper case character, at the beginning of the name.
Let’s setup our directory structure:
Magento requires there to be an XML file that tells Magento to look for and use your custom module.
Also you can disable your module in the Configuration menu on the backend via the Advanced tab.
NOTE: Due to a bug in Magento, whitespace is not treated correctly. This means that if you leave space in the values between node names (anything in angled brackets <> is a node), Magento will break.
As an explanation of the above code you will see that all you are changing is the [Namespace]_[Module] text and leaving everything else the same. Please note the capital P in codePool. If this is lowercase this module will not be active.
NOTE: You may notice that there is no closing,
?>, PHP tag in the code. This is a common coding style that Magento core classes use. Magento Coding Standard is similar (with some exceptions) to Zend Framework PHP Coding Standard and you can find the detailed explanations of this rule in Zend Framework Documentation
NB : You can use the frontName of your choice without any link to your module name. IE : Mage_Catalog could have “mycatalog” as a frontName.
If you are quite new to Magento you should pay attention to one of its specifics! The Constructors below are not the usualPHP-Constructors!! Keeping that in mind can save hours of frustrating crashes ;)
NOTE: The ‘<module>_id’ refers to the PRIMARY KEY in your database table.
NOTE: Please note the <module> text that needs to be replaced. This SQL structure is up to you, this is merely a starting point.
Note Important: If you add fields and couldn’t save data in these fields please try to go to System→Cache Management Then 1.Flush Cache Storage 2.Flush Magento Cache.
NOTE: The block type will automatically figure out what template file to use based on the second [module] declaration.
As an alternate way of declaring what template file to use you can use this:
NOTE: Uncomment anything that you would like to use and this is just a starting point and some common methods for you to try and pull the data out.
In this section I am utilizing the built-in Grid Widgets and form capabilities to create a form to allow editing and creating new items for your custom database.
Here is the revised directory setup due to the additions and changes we need for the backend module.
These control the setup and appearance of your grids and the options that they display.
NOTE: Please note the fact that Block comes before Adminhtml in the class declaration. In any of the Magento modules in Adminhtml it is the opposite. For your module to work it has to be Block_Adminhtml otherwise you will get a ‘Cannot redeclare module...’ error.
NOTE: you need to manually add line 16, which is currently missing in this file.
It’s also worth noting the adminhtml changes in the config.xml (above) can be placed in their own XML file instead, keeping these changes separated away:
Also, rather than using a rewrite for the admin section described above, you can implement the same standard admin generated urls Magento uses. These look like: ‘/admin/[module]/index/’ instead of the above that would generate ‘/[module]/adminhtml_[module]/index/’.
To implement this different url structure you can change the following in your config.xml: