How to extend Magento core controller?
2011-12-31 10:47
190 查看
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.
?
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:
?
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 -
?
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:
?
...and now you know how to rewrite/reroute requests to different controllers.
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.
?
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:
?
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 -
?
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:
?
相关文章推荐
- Core Data on iOS 5 Tutorial: How To Use NSFetchedResultsController
- How to extend Magento Order Grid?
- How to overload controller in Magento?
- How to embed Google Custom Search Engine in Magento
- how to install Request Tracker on Fedora Core 9
- How to batch extend VM disk
- How to block certain web sites from active directory domain controller?
- How to pass List from Controller to View in MVC 3.
- Magical Record: how to make programming with Core Data pleasant
- How to setup/configure multiple stores in Magento?
- how to read openstack code: Core plugin and resource extension
- How to Install Free or Premium Magento Themes
- Magento How To Display Product Custom Option On list.phtml
- magento如何获取产品属性值 How to Obtain Custom Attribute Values in Magento
- How to Programmatically Add/Delete Custom Options in Magento? - See more at: http://apptha.com/blog/
- How to interpret a Linux core dump file
- How to build android libcore/luni, native code?
- How to extend the root partition in LVM
- How to override Magento model classes?
- How to keep CoreBluetooth connection alive between views