Controller Init() Issue

I am trying to (pre)setup the language of an Yii2 app. So I decided to create a BaseController extending yii\web\Controller and all my custom controllers would extend BaseController (all good thus far). In BaseController I re-defined init() method so that I can (pre)set the app language. In short I check if language parameter is passed via url. If it is I set the language if not I set it to default language. All works fine except on the baseUrl. When only the baseUrl is rendered the init() method does not seem to be called at all.

By default the app renders site/index and when rendering just the baseUrl it does render site/index with no problem but does not call init() method in my base controller. The question is what am I missing? Is there a better place to preset the language before render the action?

You can use beforeAction (see APi docs for correct usage).

forgot to mention I tried beforeAction($action). Doesn’t work either. Thanks anyway ORey

I think my vagrant box was messed up. Re-built it and all is working again!