Magento: How to Override Mage_Core_Controller_Fron

2019-02-15 12:48发布

问题:

I'd like to override Mage_Core_Controller_Front_Action to add ACL checks to all front-end controller actions, similar to the way it's done in Mage_Adminhtml_Controller_Action::preDispatch() using Mage_Adminhtml_Controller_Action::_isAllowed().

I've been able to create a PackageName_ModuleName_Controller_Front_Action that successfully extends Mage_Core_Controller_Front_Action, include, and use it from any controller, however, this method would require that I now extend every Magento front-end controller that I want to add ACLs to in order to get them to use my new Front_Action. If I want a Deny, Allow priority policy setup, that's a lot of overriding.

If anyone knows how to properly extend this class in a more efficient manner it would certainly come in handy, otherwise I suppose I could work on an Allow, Deny priority, but that's still going to be a lot of overriding for my use case.

回答1:

Instead of overriding Mage_Core_Controller_Front_Action (which is almost impossible without, as you mentioned, overriding each front controller that is based on this class), you can put an observer on the controller_action_predispatch event, and use the controller_action value that is sent with it (and which should actually be on front-end the current instance of Mage_Core_Controller_Front_Action).