dongshan6870 2018-03-07 17:12
浏览 277
已采纳

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');
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dongshi1914 2018-03-07 23:16
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog