INSYNC New Logo

Creating Custom Module in Magento

Creating Custom Module in Magento -banner

The first step in creating custom module in Magento 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.

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

integrate magento with erp

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 /app/etc/modules/MyCompanyName_WelcomeInsync.xml

Activate module in Magento


integrate magento with erp

MyCompanyName is a unique namespace for your modifications, it doesn’t have to be your company’s name, but that the recommended convention my magento. WelcomeInsync is the name of your module.Clear the application cache.

Now that the module file is in place, we’ll need to let Magento know about it (and check our work). In the admin application

  • Go to System->Cache Management
  • Select Refresh from the All Cache menu
  • Click Save Cache settings

Now, we make sure that Magento knows about the module

  • Go to System->Configuration
  • Click Advanced
  • In the “Disable modules output” setting box, look for your new module named “MyCompanyName_ WelcomeInsync”


integrate magento with erp

If you can live with the performance slow down, you might want to turn off the application cache while developing/learning. Nothing is more frustrating then forgetting the clear out the cache and wondering why your changes aren’t showing up. Setup the directory structure

Next, we’ll need to setup a directory structure for the module. You won’t need all these directories, but there’s no harm in setting them all up now.

  • app/code/local/MyCompanyName/WelcomeInsync
  • app/code/local/MyCompanyName/WelcomeInsync/Block
  • app/code/local/MyCompanyName/WelcomeInsync/controllers
  • app/code/local/MyCompanyName/WelcomeInsync/Model
  • app/code/local/MyCompanyName/WelcomeInsync/Helper
  • app/code/local/MyCompanyName/WelcomeInsync/etc
  • app/code/local/MyCompanyName/WelcomeInsync/sql

And add a configuration file

app/code/local/MyCompanyName/WelcomeInsync/etc/config.xml and inside the configuration file, add the following, which is essentially a “blank” configuration.


integrate magento with erp

Create Configuration XML

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

Create Configuration XML

Oversimplifying things, this configuration file will let you tell Magento what code you want to run. Setting up the router. Next, we need to setup the module’s routers. This will let the system know that we’re handling any URLs in the form of

So, in your configuration file, add the following section.

Set Up Magento Custom Module Router

What you’re saying here is “any URL with the frontName of welcomeinsync…

should use the frontName controller “MyCompanyName_WelcomeInsync

So, with the above configuration in place, when you load the WelcomeInsync page above, you’ll get a 404 page. That’s because we haven’t created a file for our controller. Let’s do that now.

app/code/local/MyCompanyName/WelcomeInsync/controllers/IndexController.php

Now try loading the page. Progress! Instead of a 404, you’ll get a PHP/Magento exception Controller file was loaded but class does not exist


integrate magento with erp

So, open the file we just created, and paste in the following code. The name of the class needs to be based on the name you provided in your router.

Class and Router in Configuration XML of magento custom module

What we’ve just setup is the module/frontName controller. This is NOT the MVC controller. Try the following URL, and you’ll get a 404, even if you had a testAction method in MyCompanyName_ WelcomeInsync_IndexController

welcomeinsync/test

So, to setup the MVC controller, create a new file, and enter the following code (Yes, this also extends the Mage_Core_Controller_Front_Action controller, even though it’s not a Front Action. Yes, this is confusing, but I think it’s how things are done)

app/code/local/MyCompanyName/ WelcomeInsync/controllers/TestController.php

Set up MVC Controller for Magento Custom module

You should now be able to hit the following URLs and see the results of your echo statements

So, that should give you a basic idea on how Magento dispatches to a controller. From here I’d recommended poking at the existing Magento controller classes to see how models and the template/layout system should be used.


integrate magento with erp

You may also like:
Connect Magento Community 1.9 with SAP Business One and MS Dynamics NAV
How to Get your Magento Store Indexed on Google easily
When and Why should you upgrade to Magento 2.x?

Search

Search
On Key

Related Posts

APPSeCONNECT is now a completely new entity headquartered in the US! 🇺🇸