How to call a method before the method of requeste

2019-05-10 03:59发布

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.

2条回答
淡お忘
2楼-- · 2019-05-10 04:12

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.

查看更多
做个烂人
3楼-- · 2019-05-10 04:28

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
    }
查看更多
登录 后发表回答