How do I create a module in Magento

How do I create a simple "Hello World" module in Magento?

First and foremost, I recommend purchasing the PHP Architect PDF / E-Book. It costs $ 20, but it's the only direct resource I could find: "How Magento Works". I also started writing Magento tutorials on my own website.

Second, if you have a choice and are not an experienced programmer or do not have access to an experienced programmer (ideally in PHP and Java), choose another car. Magento is mature, but it was developed as a shopping cart solution on which other programmers can build modules. It was not designed to be easily understood by people who are smart but not programmers.

Third, Magento's MVC is very different from the Ruby on Rails MVC, Django, CodeIgniter, CakePHP, etc. model that is popular with PHP developers these days. I think it's based on the Zend model and it's very Java OOP-like. There are two Controllers to worry about. The module / FrontName controller and then the MVC controller.

Fourth, the Magento application itself is built using the same modular system that you are using. So rummaging through the core code is a useful learning tactic. Also, a lot of what you will be doing with Magento is overwrite existing classes. What I'm talking about here is Create new functionality, do not overwrite. Keep this in mind when looking at the code samples.

I'll start with your first question and show you how to set up a controller / router to respond to a specific url. This is going to be a little novel. I may have time for the model / template related topics later, but I don't have any right now. However, I'll briefly address your SQL question.

Magento uses an EAV database architecture. Whenever possible, try to use the system-supplied model objects to get the information you need. I know everything is in the SQL tables, but it is best not to think about collecting data with raw SQL queries or you will go crazy.

Last disclaimer. I've been using Magento for about two or three weeks, so be careful Emptor. This is an exercise to get this clear in my head and to help out with the stack overflow.

Create a module

All additions and adjustments to Magento are made via modules. The first thing you need to do is create a new module. Create an XML file in with the following name

MyCompanyName is a unique namespace for your changes. It doesn't have to be the name of your company, but the recommended convention my magento. is the name of your module.

Clear the application cache

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

  1. Go to System-> Cache Management
  2. From the All Caches menu, choose Refresh
  3. Click Save Cache Settings

Now let's make sure Magento knows about the module

  1. Go to System-> Configuration
  2. Click on Advanced
  3. In the "Disable module output" settings field, look for your new module named "MyCompanyName_HelloWorld".

If you can live with a drop in performance, you may want to turn off application cache while developing / learning. Nothing is more frustrating than forgetting to clear your cache and wondering why your changes aren't showing up.

Set up the directory structure

Next we need to set up a directory structure for the module. You won't need all of these directories, but there's no harm in having them all set up now.

And add a configuration file

add the following in the configuration file, which is essentially an "empty" configuration.

You can use this configuration file to tell Magento the code that you want to run.

Set up the router

Next we need to set up the module's routers. This informs the system that we are processing URLs in the form of

Add the following section to your configuration file.

What you are saying here is "any url with the front name of helloworld ...

should use the frontName controller MyCompanyName_HelloWorld ".

With the above configuration, you will get a 404 page when you load the helloworld page above. That's because we didn't create a file for our controller. Let's do that now.

Now try to load the page. Progress! Instead of a 404, you will get a PHP / Magento exception

So open the file you just created and paste the following code. The name of the class must be based on the name you specified in your router.

What we just set up is the Module / FrontName controller. This is the standard control and action of the module. If you want to add controllers or actions, the thing to remember is that the first part of the tree of a Magento URL is immutable. They always do it that way

So if you want to match this url

You need to have a FooController, which you can do as follows:

Please note that the standard controller IndexController and the standard action indexAction can be implicit, but must be explicit if something comes afterwards. So with the controller FooController and the action indexAction and NOT with the action fooAction of the indexController will match. If you want to have a fooAction, you have to call this controller explicitly in the controller IndexController as follows: because the second part of the URL is and always will be the controller name This behavior is inherited from the Zend Framework included in Magento.

You should now be able to go to the following urls and see the results of your echo instructions

That should give you a basic idea of ​​how Magento ships to a controller. From here I recommended browsing the existing Magento controller classes to see how models and the template / layout system should be used.