Yii2将行为附加到Application :: EVENT

编程入门 行业动态 更新时间:2024-10-17 07:35:00
Yii2将行为附加到Application :: EVENT_BEFORE_REQUEST(Yii2 Attach behavior to Application::EVENT_BEFORE_REQUEST)

我正在尝试检查Application::EVENT_BEFORE_REQUEST上的cookie。 我所做的是从Behavior模型中覆盖events函数,并在上面提到的事件上返回自定义函数checkLanguage 。 我在我的控制器中as beforeRequest触发器(首先我尝试在backend/config/main.php但似乎无法从那里CheckIfLoggedIn类),并且请求发送到public function events()中CheckIfLoggedIn类,但不会继续执行checkLanguage函数。 这是我的SiteController行为:

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

和CheckIfLoggedIn.php类:

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

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

最满意答案

问题是你正试图在控制器级别附加一个Application事件,而文档说你应该使用Application配置。

进行以下更新,从类中删除events()函数。

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

并将以下内容添加到common/configbackend/config如果您希望仅用于backend

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

记得在id或components索引被定义为这样的同一级别添加它

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

对于第一次尝试添加一个die("hello world"); 在changeLanguage的开头,所以你可以确认它正在进入函数changeLanguage 。

希望能帮助到你

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

更多推荐

本文发布于:2023-07-26 00:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268400.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Application   EVENT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!