Run Code At Startup Of Yii Application

I need an affiliate ad tracking code block that will run every page no matter what (it has to).

I have currently put it into the session class, extending it into my own application adding the code to the init() function.

The code uses application parts like: Yii::$app->user->identity in the init() function of the Session class.

I have since learnt this is not a good way of doing it however, I do not wish to create a specific controller for all this.

But I also cannot seem to figure out how to use before request events.

Anyone got any idea?

1.you can use before request events such as :

in config file




.......


$config = [

        ................


	'id' => 'basic',

	'basePath' => dirname(__DIR__),

	'language' =>'zh-CN',

	'extensions' => require(__DIR__ . '/../vendor/yiisoft/extensions.php'),

	'bootstrap' => ['log'],

	'on beforeRequest' => ['app\models\Admin','checkAccess'],//your beforeRequest event callback

        ...................


]

.....




  1. you can write a class which extends yii\base\Component



use yii\base\Component;


class SomeClass extends Component{

    ........

}


/******in config file*****/

$config = [

        ................


	'id' => 'basic',

	'basePath' => dirname(__DIR__),

	'language' =>'zh-CN',

	'extensions' => require(__DIR__ . '/../vendor/yiisoft/extensions.php'),

	'bootstrap' => ['log','someClass'],

        ...................

       

         'components' => [

            'someClass'=>[

               'class'=>'someClass',

             ]

         ]

]




Thanks the first one was what I was looking for.