I am creating this blog which will let you understand how to create a module in Magento 1.*
There's two controllers you need to be concerned about. The module/frontName controller, and then the MVC controller.
Lets start and know how to setup a controller/router to respond to a specific URL.
Step 1: Create a module
If you want to customize Magento and add new things then best way to achieve it is through modules. So, the first thing you'll need to do is create a new module. Create an XML file in app/modules
named as follows
cd /path/to/store/app
touch etc/modules/MyCompanyName_HelloWorld.xml
<?xml version="1.0"?>
<config>
<modules>
<MyCompanyName_HelloWorld>
<active>true</active>
<codePool>local</codePool>
</MyCompanyName_HelloWorld>
</modules>
</config>
HelloWorld
is the name of your module.
- MyCompanyName is a unique namespace for your modifications,
Clear the application cache
Now module files is in the place , now we'll need to let Magento know about it by going to Admin panel of Magento:
- 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_HelloWorld"
Step 2: 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.
mkdir -p app/code/local/MyCompanyName/HelloWorld/Block
mkdir -p app/code/local/MyCompanyName/HelloWorld/controllers
mkdir -p app/code/local/MyCompanyName/HelloWorld/Model
mkdir -p app/code/local/MyCompanyName/HelloWorld/Helper
mkdir -p app/code/local/MyCompanyName/HelloWorld/etc
mkdir -p app/code/local/MyCompanyName/HelloWorld/sql
And add a configuration file
touch app/code/local/MyCompanyName/HelloWorld/etc/config.xml
and inside the configuration file, add the following, which is essentially a "blank" configuration.
<?xml version="1.0"?>
<config>
<modules>
<MyCompanyName_HelloWorld>
<version>0.1.0</version>
</MyCompanyName_HelloWorld>
</modules>
</config>
Step 3: 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
http://example.com/magento/index.php/helloworld
So, in your configuration file, add the following section.
<config>
<!-- ... -->
<frontend>
<routers>
<!-- the <helloworld> tagname appears to be arbitrary, but by
convention is should match the frontName tag below-->
<helloworld>
<use>standard</use>
<args>
<module>MyCompanyName_HelloWorld</module>
<frontName>helloworld</frontName>
</args>
</helloworld>
</routers>
</frontend>
<!-- ... -->
</config>
What you're saying here is "any URL with the frontName of helloworld "
http://example.com/magento/index.php/helloworld
should use the frontName controller MyCompanyName_HelloWorld".
Now you'll get a 404 page with the above configuration in place. This will happen because we haven't created a file for our controller. Do this by creating IndexController.php in the following directory
touch app/code/local/MyCompanyName/HelloWorld/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
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 MyCompanyName_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action{
public function indexAction(){
echo "We're echoing just to show that this is what's called, normally you'd have some kind of redirect going on here";
}
}
module/frontName controller have been setup now, this is the default controller and default action of the module.
If you want to add controllers or actions then you have to remember this
http://example.com/magento/index.php/frontName/controllerName/actionName
So if you want to match this url
http://example.com/magento/index.php/helloworld/foo
You will have to have a FooController, which you can do this way :
touch app/code/local/MyCompanyName/HelloWorld/controllers/FooController.php
class MyCompanyName_HelloWorld_FooController extends Mage_Core_Controller_Front_Action{
public function indexAction(){
echo 'Foo Index Action';
}
public function addAction(){
echo 'Foo add Action';
}
public function deleteAction(){
echo 'Foo delete Action';
}
}
Call this controller IndexController explicitly like this way: http://example.com/magento/index.php/helloworld/index/foo
if you want to have a fooAction in the controller IndexController.
Now following urls will be working
http://example.com/magento/index.php/helloworld/foo
http://example.com/magento/index.php/helloworld/foo/add
http://example.com/magento/index.php/helloworld/foo/delete
Thats all.
Thanks for reading the blog.
0 Comment(s)