How to call a method before the method of requested controller is executed : magento

安稳与你 提交于 2019-12-22 11:04:14

问题


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
    }


来源:https://stackoverflow.com/questions/4003450/how-to-call-a-method-before-the-method-of-requested-controller-is-executed-mag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!