Custom bootstrap code

I have some code that has to run everytime Yii starts, for example I have a function that checks whether a user is using mobile or desktop, this sould be stored in a var that is accessible in the controller. The code needs to run after Yii has initialized, but before the MVC functionalities are deployed. What is the best location to put this code?

I tried to use the init() method in the controller component, but it looks like that method isn’t called.

Attach an event handler to onBeginRequest event of CWebApplication. This wiki article may help to implement it.