Page tree
Skip to end of metadata
Go to start of metadata

Module Creator

 

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) 

Download Module Creator (v0.0.9.1) 

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’.

The directories:

  1. /new/app/design/adminhtml/interface/theme/
  2. /new/app/design/frontend/interface/theme/

In a new Magento installation correspond to:

 

  1. /app/design/adminhtml/default/default/
  2. /app/design/frontend/default/default/

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.

Installing the Module Creator

 

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:

  1. ../magento

The ‘..’ signifies one directory up.

Document Conventions

 

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.

Create Directories

 

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:

/app/code/local

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:

/app/code/local/<Namespace>/<Module>/

  1. Block/
  2. controllers/
  3. etc/
  4. Model/
  5.     Mysql4/
  6.         <Module>/
  7. sql/
  8.     <module>_setup/

/app/design/frontend/<interface>/<theme>/

  1. template/
  2.     <module>/

Activate Module

 

Magento requires there to be an XML file that tells Magento to look for and use your custom module.

/app/etc/modules/<Namespace>_<Module>.xml

 Expand source

 

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.

Create Controller

 

/app/code/local/<Namespace>/<Module>/controllers/IndexController.php

 Expand source

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

Create Configuration XML

 

/app/code/local/<Namespace>/<Module>/etc/config.xml

 Expand source

 

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.

Create Helper

 

/app/code/local/<Namespace>/<Module>/Helper/Data.php

 Expand source

Create Models

 

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 ;)

/app/code/local/<Namespace>/<Module>/Model/<Module>.php

 Expand source

 

/app/code/local/<Namespace>/<Module>/Model/Mysql4/<Module>.php

 Expand source


NOTE: The ‘<module>_id’ refers to the PRIMARY KEY in your database table.

/app/code/local/<Namespace>/<Module>/Model/Mysql4/<Module>/Collection.php

 Expand source

SQL Setup

 

/app/code/local/<Namespace>/<Module>/sql/<module>_setup/mysql4-install-0.1.0.php

 Expand source

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.

Template Design

 

/app/design/frontend/<interface>/<theme>/layout/<module>.xml

 Expand source

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:/app/design/frontend/<interface>/<theme>/layout/<module>.xml

 Expand source

 

/app/design/frontend/<interface>/<theme>/template/<module>/<module>.phtml

 Expand source

 

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.

 

Directory Additions

 

 

Here is the revised directory setup due to the additions and changes we need for the backend module.

/app/code/local/<Namespace>/<Module>/

  1. Block/
  2.     Adminhtml/
  3.         <Module>/
  4.             Edit/
  5.                 Tab/
  6. controllers/
  7.     Adminhtml/
  8. etc/
  9. Helper/
  10. Model/
  11.     Mysql4/
  12.         <Module>/
  13. sql/
  14.     <module>_setup/

 

Blocks

 

 

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.

/app/code/local/<Namespace>/<Module>/Block/Adminhtml/<Module>.php

 Expand source
/app/code/local/<Namespace>/<Module>/Block/Adminhtml/<Module>/Edit.php

 

 Expand source
/app/code/local/<Namespace>/<Module>/Block/Adminhtml/<Module>/Grid.php

 

 Expand source

 

/app/code/local/<Namespace>/<Module>/Block/Adminhtml/<Module>/Edit/Form.php

 Expand source

/app/code/local/<Namespace>/<Module>/Block/Adminhtml/<Module>/Edit/Tabs.php

 

 

 

 Expand source

 

/app/code/local/<Namespace>/<Module>/Block/Adminhtml/<Module>/Edit/Tab/Form.php

 Expand source


Controller

 

/app/code/local/<Namespace>/<Module>/controllers/Adminhtml/<Module>Controller.php

NOTE: you need to manually add line 16, which is currently missing in this file.

 

 Expand source

XML Configuration Changes

 

/app/code/local/<Namespace>/<Module>/etc/config.xml

 Expand source

XML Layout

 

/app/design/adminhtml/<interface>/<theme>/layout/<module>.xml

 Expand source

Seperate Adminhtml Configuration

 

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:

/app/code/local/<Namespace>/<Module>/etc/adminhtml.xml

 Expand source

Standard Magento Admin URLs, no rewrite needed

 

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:

/app/code/local/<Namespace>/<Module>/etc/config.xml

 Expand source

 

 

 

 


 

 

  • No labels