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');
}
}
}