覆盖 Magento 操作

发布于 2024-11-02 02:42:27 字数 917 浏览 0 评论 0原文

很多时候,我想做的只是覆盖控制器上的特定操作,而不是整个操作。在大多数情况下,我只是覆盖整个控制器,但我想知道是否有更好的方法? Magento 是否有办法只覆盖控制器中的单个操作,而使原始控制器和其他操作保持原样?

示例:

class Mage_Core_AwesomeController extends 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
    }

}

所以 url Awesome/torewrite 将转到 Local_Core_AwesomeController,但 url Awesome/index 将转到 Mage_Core_AwesomeController。

这个例子显然是捏造的,它只是为了展示我理论上想要的东西。因此,请不要尝试纠正该示例,只需演示覆盖某个操作的最佳方法即可。

我认为还需要注意的是,我不想重写 url,只是覆盖操作。也许如果不重写 url 这是不可能的?只是当重写 url 时,布局中的标签发生了变化,我宁愿保持它们相同。

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 extends 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染墨丶若流云 2024-11-09 02:42:27

Local/Core/etc/config.xml 中,定义要覆盖的路由器内的控制器。

<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 现在将在 Mage/Core/controllers 之前检查 Local/Core/controllerscore (路由器名称)开头的 URL 路径。上面的 PHP 类已经是正确的。

这仅在此页面的中间位置轻轻暗示说:

从 Magento 1.3 开始,您只需将模块添加到前端路由器即可。不再需要重写。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文