Set Default Setting For Yii2

Hi all,

I’m wondering how to set the default settings for application before any action of the controller is execute ?

For example, I want to change the timezone of the application based on the user settings, and I want to set them before the execution of any actions.

Any ideas ?


class SiteController extends Controller


    public function beforeAction($action)


        // do stuff

        return parent::beforeAction($action);



You may want to consider creating a BaseController so you don’t need to repeat this function.

Thanks amnah for quick reply

You can do this in the different ways:

  1. set your custom handler for Application::EVENT_BEFORE_REQUEST or Application::EVENT_BEFORE_ACTION

  2. you can create an application component with work you need in it’s init() method. And adjust this component to be the preload one