Yii2将行为附加到Application :: EVENT_BEFORE_REQUEST

I am trying to check for a cookie on Application::EVENT_BEFORE_REQUEST. What I did is overriding the events function from Behavior model and return a custom function checkLanguage on the event that I mentioned above. I am triggering as beforeRequest in my controller ( in first I tried in the backend/config/main.php but it seems that the CheckIfLoggedIn class can't be reached from there ) and the request goes e to the public function events() in the CheckIfLoggedIn class but doesn't go on to the checkLanguage function. This is my SiteController behaviors:

public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'index', 'language'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
            'as beforeRequest' => [
                'class' => 'backend\components\CheckIfLoggedIn'
            ]
        ];
    }

and CheckIfLoggedIn.php class:

<?php

namespace backend\components;

use yii\base\Behavior;
use yii\web\Application;

class CheckIfLoggedIn extends Behavior
{
    public function events()
    {
        return [
            Application::EVENT_BEFORE_REQUEST => "changeLanguage"
        ];
    }

    public function changeLanguage()
    {
        if(\Yii::$app->getRequest()->getCookies()->has('lang')){
            \Yii::$app->language = \Yii::$app->getRequest()->getCookies()->getValue('lang');
        }
    }
}

The thing is you are trying to attach an Application event at controller level inside the behavior whereas the documentation says you should use the Application config .

Make the following updates, remove the events() function from your class.

backend/components/CheckIfLoggedIn.php

namespace backend\components;
use yii\base\Behavior;

class CheckIfLoggedIn extends Behavior
{

    public function changeLanguage()
    {
        if(\Yii::$app->getRequest()->getCookies()->has('lang')){
            \Yii::$app->language = \Yii::$app->getRequest()->getCookies()->getValue('lang');
        }
    }
}

and add the following to the common/config or backend/config if you want it for backend only

'on '.yii\web\Application::EVENT_BEFORE_REQUEST => [
    'backend\components\CheckIfLoggedIn','changeLanguage'
] ,

remember to add it at the same level where id or components index is defined like this

return [
    'id' => 'app-backend' ,
    'on '.yii\web\Application::EVENT_BEFORE_REQUEST => [
        'backend\components\CheckIfLoggedIn','changeLanguage'
    ] ,

For the first try add a die("hello world"); in the start of changeLanguage so you can confirm it is entering the function changeLanguage.

Hope it helps