How to extend Magento core controller?

Just as with models/blocks/helpers, there are times when we want to change the way a core Magento controller works. Since we DON'T EVER WANT TO CHANGE THE CORE, we need a way to tell a request to use OUR controller instead of Magento's controller. We do this by changing the configurations that hold the routing instructions.

Many MVC frameworks use the concept of a route to tell the application how to map the URL to the correct controller/action. Magento has part of that mapping in the config.xml. Below is an example of a common route configuration for the frontend.

?
1
2
3
4
5
6
7
8
9
10
11
< frontend >
    < routers >
       < awesome >
            < use >standard</ use >
            < args >
               < module >Super_Awesome</ module >
               < frontName >awesome</ frontName >
            </ args >
       </ awesome >
    </ routers >
</ frontend >

With that frontend route, a url that looks like this: http://example.com/awesome/noun/verb will be routed to the NounController's verbAction() inside the Super_Awesome module.

In our example, I want to dispatch an event at the beginning of the indexAction(), but I don't want to make ANY other changes. To do this, I will have to create my controller that extends the Mage_Checkout_OnepageController and override the indexAction().

But how do I tell Magento to use my controller instead of Mage_Checkout_OnepageController? Well, that is the point of this post.

First, we need to create our controller:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
 
require_once 'Mage/Checkout/controllers/OnepageController.php' ;
 
class Super_Awesome_OnepageController extends Mage_Checkout_OnepageController
{
     public function indexAction()
     {
         Mage::dispatchEvent( 'awesome_event' , array ( 'piece of data' ));
         parent::indexAction();
 
     }
}

The require_once is important. If you don't do this, your code will blow up. How we need to tell Magento to change it's route mappings.

Here is the piece of the config.xml where we do just that -

?
1
2
3
4
5
6
7
8
9
10
11
12
< frontend >
     < routers >
         < checkout >
             < use >standard</ use >
             < args >
                < modules >
                   < Super_Awesome before = "Mage_Checkout" >Super_Awesome</ Super_Awesome >
                </ modules >
             </ args >
         </ checkout >
     </ routers >
</ frontend >

If we were to write this in English, it would simply say, "When a request comes in with "checkout" as the module, first look for whatever controller it is requesting in Super_Awesome before looking in Mage_Checkout. If you don't find it in Super_Awesome, fall back on Mage_Checkout".

For this to work, the Controller has to be named the same.

If you are wanting to reroute an adminhtml controller, you would use the same concept. If I have a module called Super_Awesome, I tend to create a Adminhtml directory under controllers to house any of my Admin controllers. The directory structure would be: Super/Awesome/controllers/Adminhtml/NounController.php.

So in the config.xml, I would have something equivalent to:

?
1
2
3
4
5
6
7
8
9
10
11
< admin >
     < routers >
         < adminhtml >
             < args >
                 < modules >
                     < awesome before = "Mage_Adminhtml" >Super_Awesome_Adminhtml</ awesome >
                 </ modules >
             </ args >
         </ adminhtml >
     </ routers >
</ admin >

...and now you know how to rewrite/reroute requests to different controllers.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值