Is there a way to intercept a request before a controller action is run, so that you could preform additional authorisation checks?

Say I have an authorised user, but their access to actions is dependant on a 3rd party API. So I need a way to check, this 3rd party API or a stored session version, before each action is performed.

I can see the beforeAction() method, which is fine for new Controllers I have added, but I need site wide solution that will catch request that go through Craft and doesn't touch any plugins.

I can see the onBeginRequest Event, but aware this fires before Craft has had chance to setup. So I feel this might not be the best solution for me, as I would like to use services inside custom plugins. If there is no other way, I assume this is best place to look. If so what is the best way to add an Event listener for this?

1 Answer 1


You're correct about both beforeAction() and onBeginRequest().

However, you could put an init() method in your main plugin class file and this will execute on every request as long as the plugin is installed and enabled.

public function init()
    // Whatever code you were going to put in onBeginRequest can go here.
  • I had considered this and even started to write code! However it made me feel like I was hacking at it, so I stopped. Thanks for letting us know that at present I wasn't far off the mark!
    – PanPipes
    Commented Oct 6, 2015 at 8:46

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.