覆盖 Magento 操作
很多时候,我想做的只是覆盖控制器上的特定操作,而不是整个操作。在大多数情况下,我只是覆盖整个控制器,但我想知道是否有更好的方法? 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
Local/Core/etc/config.xml
中,定义要覆盖的路由器内的控制器。Magento 现在将在
Mage/Core/controllers
之前检查Local/Core/controllers
以core
(路由器名称)开头的 URL 路径。上面的 PHP 类已经是正确的。这仅在此页面的中间位置轻轻暗示说:
In your
Local/Core/etc/config.xml
, define your controller within the router to be overridden.Magento will now check
Local/Core/controllers
beforeMage/Core/controllers
for URL paths starting withcore
(the router name). Your PHP class above is already correct.This is only gently hinted at about halfway down this page where it says: