4

I have module with license based.I have to check license is valid or not in my all controllers.

Now Actually i'm placing my logic in each action.

Is there any better way to check logic & redirect to different page if license is not valid.

2 Answers 2

4

For all actions of a single controller class, you can override the preDispatch or postDispatch methods:

class YourController extends Mage_Adminhtml_Controller_Action
{
    public function preDispatch()
    {
        // put logic before controller actions here
        parent::preDispatch();
    }
    public function postDispatch()
    {
        // put logic after controller actions here
        parent::preDispatch();
    }
}

If all controllers of your module inherit from a single abstract controller, you only have to write this once in that abstract controller.

In theory you can also use the controller_action_predispatch_{$frontname}_{$controller}_{$action} events, but those are more useful if you want to add custom logic to other modules.

2
  • if i use redirect in predispatch method session messages not displaying. Aug 31, 2015 at 5:59
  • That's because the action will still be dispatched and the session messages rendered, but invisible to the user because he gets redirected. To prevent further dispatching, $this->getRequest()->setDispatched(true); should do the trick. Aug 31, 2015 at 6:17
0

fschmengler was explained nicely.

But my suggestion to use controller_action_predispatch event if use event

If use controller_action_predispatch_{$frontname}_{$controller}_{$action} then servers times need to write observer for each action and each controller in config.xml.

controller_action_predispatch_{$frontname}_{$controller}_{$action} is best whenever it use on one Action.

If we use controller_action_predispatch event put frontenName as checking condition as a condition on observer

$route=$observer->getEvent()->getControllerAction()->getRequest()->getRouteName();

if(Mage::app()->getFrontController()->getRouterByRoute($route)=='YOurModuleFrontName' ):

// Do your code;
endif;

then it will be reduce code in config.xml

3
  • 1
    This works, but the controller_action_predispatch is dispatched for every page. So observing this is going to introduce a small overhead. It's better to do it via preDispatch() method.
    – Marius
    Aug 28, 2015 at 13:00
  • yes.. predispatch is good process. But i am talking about event... not on preDispatch() function.May be this case,you can agree with me! :)
    – Amit Bera
    Aug 28, 2015 at 13:03
  • In cases where the observer is needed, it's true, that's the way to do it, but here it's about doing something in a custom controller. Not a core one.
    – Marius
    Aug 28, 2015 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.