开发者

Overriding a Magento Action

There has been many times when all I want to do is override a specific action on a controller but not the whole thing. In most cases I have just overrode the whole controller, but I'm wondering if there is a better way? Does Magento have a way to just override a single action in a controller leaving the original controller and other actions as they were?

Example:

class Mage_Core_AwesomeController exten开发者_JS百科ds Mage_Core_Controller_Front_Action {

    //has url of awesome/index
    public function indexAction(){
        //Some Awesome code
    }

    //has url of awesome/torewrite
    public function torewriteAction(){
        //Some Awesome code
    }

}

class Local_Core_AwesomeController extends Mage_Core_AwesomeController {

    //has url of awesome/torewrite
    public function torewriteAction(){
        //Some Awesome Override code
    }

}

So the url awesome/torewrite would go to Local_Core_AwesomeController but the url awesome/index would go to Mage_Core_AwesomeController.

This example is obviously fabricated, its merely there to show what I would want in theory. So please don't try and correct the example, just demonstrate the best way to override just an action.

I think it would also be important to note that I do not want to rewrite the url, just override the action. Maybe this is impossible without rewriting the url? Its just that when rewriting the url the tags in the layout change and I would rather keep them the same.


In your Local/Core/etc/config.xml, define your controller within the router to be overridden.

<config>
    ...
    <frontend> // Use <admin> for backend routers
        <routers>
            <core> // <-- this is the router name
                <args>
                    <modules>
                        <local_core before="Mage_Core">Local_Core</local_core>
                    </modules>
                </args>
            </core>
        </routers>
    </frontend>
    ...
</config>

Magento will now check Local/Core/controllers before Mage/Core/controllers for URL paths starting with core (the router name). Your PHP class above is already correct.

This is only gently hinted at about halfway down this page where it says:

Since Magento 1.3 you can simply add your module to the frontend router. Rewrites are not neccessary any more.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜