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

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 qc代码,修改和添加东西
  • ¥50 Unity的粒子系统使用shadergraph(内置管线)制作的一个顶点偏移shader,但是粒子模型移动时,顶点也会偏移
  • ¥15 如何用python处理excel的数据(极值标准化)
  • ¥15 三向应力状态求剪应力
  • ¥15 jupyter notebook如何添加libGL.so.1库
  • ¥20 easyPoi能否实现下拉多选或者复选框
  • ¥15 网桥在转发帧时,会变帧的源地址和目的地址吗?
  • ¥15 用Multisim设计汽车尾灯控制电路
  • ¥100 求用matlab求解上述微分方程的程序代码
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码