Yii::$App Gets Reset Every Time Reentering Index.php


I was watching Yii::$app value in the debug watch window and noticed that the value became null every time /index.php (see below code) is loaded/entered. Does anyone have any idea what may cause this problem. Is this expected or an issue? It seems like due to this problem, I can persist my custom login and authentication. Because Yii::$app->user became null too.

Please help! Thanks!


// comment out the following two lines when deployed to production

defined('YII_DEBUG') or define('YII_DEBUG', true);

defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../vendor/autoload.php');

require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

$config = require(__DIR__ . '/../config/web.php');

(new yii\web\Application($config))->run();

I think is expected, but user login/authentication should be persistent as values are stored in session.

It is how PHP works. In the end of each request everything is destroyed and you have to re-initialize everything from scratch.