Pages

Tuesday, August 25, 2015

Getting Started: Development

Next we will create our module configuration file. This file will tell Magento all about our module. This includes how many files our module contains, what type of files (models, helpers, database classes), and so on.
Go to app/code/local/Chiragdodia/Mymodule/etc and create a config.xml file that will contain following content
Let's go through line by line to understand each tag. Here, the first tag is <module>that contains the name and version of our module. The version number is very important when it comes to updating your module update your module.
The <frontend> tag will tell Magento about the controller dispatched. Inside the <frontend> tag, we have defined <routers> that tells Magento how to access our controllers via the routing mechanism. 
In the <mymodule> tag, we have defined module name in <module> tag and frontend name in <frontName>. By using a frontend name, we can access our module in frontend like yoursitename.com/index.php/mymodule/index.
By calling yoursitename.com/index.php/mymoduleor yoursitename.com/index.php/mymodule/index Magento will look for index action of your module's controller file. As such, we need to create our controller file.
Go to  app/code/local/Chiragdodia/Mymodule/controllers and create file IndexController.php with following content.
Note that each file's name and class names are case sensitive in Magento It's very important that you are taking care in naming your work when creating files and classes.
Now open URL yoursite.com/index.php/mymodule/index it will print "Hello tuts+ World". Awesome - we're finally done with our first hello world module.
Here we have extend the class Mage_Core_Controller_Front_Action that contains all the methods which are using in routing of url. The Magento class name reflects the location of class file. So the class Mage_Core_Controller_Front_Action resides in location Mage > Core > Controller > Front > Action.php
See the class name of our controller that is Chiragdodia_Mymodule_IndexController. Magento controller to be named in such a way that it reflects (<module>tag)_(Action Controllername)(keyword Controller).
  • <module>tag = Chiragdodia_Mymodule (we have defined this tag inconfig.xml)
  • Action Controllername = Index
  • Action controller followed by the Controller keyword 
Based on this pattern, the name of our controller is Chiragdodia_Mymodule_IndexController
Now see the URL pattern which is follow the below route pattern
yoursite.com/index.php/frontendname/actionControllername/actionmethod
  • frontendname = mymodule
  • actionControllername = Index
  • actionmethodname = Index
Based on this URL pattern, our module's URL is yoursite.com/index.php/mymodule/index/index. You can also access it using yoursite.com/index.php/mymodule because whenever you have not specified an actionController or actionmethod name, Magento loads the index controller and index action by default.
Now let's create one more action: testAction .
We can access the testAction using URL yoursite.com/index.php/mymodule/index/test. As explained earlier here
  • frontendname = mymodule
  • actionControllername = Index
  • actionmethodname = test
This is how the controller works in Magento.
At first, it can be difficult to understand everything at once so I've included all of the source code to this module so that you can review it and implement your own work while using it as a guide.
What's Next?
In next part, we are going to fill in some layouts in our module by creating layout and block files. We will understand how the layouts files works in Magento and what role blocks play within the context of Magento.
Until then create your own module and let me know of anything that causes you trouble.

No comments:

Post a Comment