Correct place for initialization code?

I’m using the Basic template to create a Yii 2.x site, and things are going great so far. I’ve managed to figure everything out.

One thing that I would like to know is what the correct way of running some code before everything else?

For example, assume I would like to run the following:




session_set_cookie_params (86400, '/', '.example.com', false, false);

ini_set ('session.gc_maxlifetime', 86400);

session_name ('cookie_name');

session_start ();


error_reporting (E_ALL);

ini_set ('display_errors', 1);


mb_internal_encoding ('UTF-8');


date_default_timezone_set ('UTC');



Would I insert this code into web/index.php? Is there somewhere else I should be placing it instead? (eg. somewhere that is always executed before the application begins)

Thanks! :)

date setting should configure in frontend config file

and other error_reporting (E_ALL); u can configure in web/index.

The example I gave wasn’t specific to dates or error reporting. What I should have said, any code that I think needs to be run before anything else, is web/index the best place to put it?

You’re saying that’s the best place? Does everyone else agree?

The code you provided above ie session, date config, etc all goes in the main config file. i.e. (here is everything that can go in the main config by default)




[

    'timeZone'=>'',

    'components' => [    	

        'session' => [

   		'class' => 'yii\web\DbSession',

            'db' => 'mydb',

            'sessionTable' => 'my_session', 

            'cookieParams'=>[

                'lifetime'=>'',

                'path'=>'',

                'domain'=>'',

                'secure'=>'',

                'httponly'=>''

 			],

        ],

        'request' => [

            'cookieValidationKey' => 'fill in a secret key here',

        ],

    ], ]




The utf stuff the charset main layout, tell your db to use it in the db config, etc…

Error reporting would be in the web index like Davee said however the log would configured in the config file.

You can do on begin request functions, before action functions, override yii2 function inits etc so it all depends on what you are trying to do.

So the proper place will be multiple places.

That’s actually sounding more complicated than it should be. I mean, I understand that it’s versatile, but it sounds like a lot…

Is this in the documentation somewhere or am I expected to figure it out on my own?

http://www.yiiframework.com/doc-2.0/guide-structure-application-components.html