How to call a method before the method of requeste

2019-05-10 04:12发布

问题:

I want to call a method before the execution of every and each controller's method. I don't want to go and call the method in every method. I just want to call it from one place and it will be called before any method of any controller in magento.

And I am sure we can do this but I don't know how it can be accomplished.

Please provide your suggestions.

Hope we can resolve this or may some expert guys already resolved this.

Thanks.

回答1:

You need to create an Observer that binds to the controller_action_predispatch Event. That will fire before every controller in the Magento codebase. There's a useful wiki page here that walks you through the process.



回答2:

You have to create a method called preDispatch in your controller. This method is executed before the requested controller action.

something like:

public function preDispatch()
    {
        parent::preDispatch();

        //my code here
    }