Yii2:独立操作在事件处理程序中不起作用
Yii2: standalone action does not work in event handler
我是 Yii 的初学者,所以不知道如何以最正确的方式解决我的问题。
有 2 个控制器 - SiteController
和 UsersController
。我需要从数据库中检索一些数据并将其输出到布局中。特别是,如果 user1 和 user2 将 user3 添加为好友,则 user3 将在主菜单面板上看到它:
无论控制器如何,用户都必须看到它,其操作是 运行(在上图中,用户看到 /site/profile
页面,但许多其他页面(特别是 invitations
)由 [= 呈现17=]控制器)。
我在 2 个控制器中编写了相同的操作:
public function getStats () { //it duplicates in UsersController and SiteController
$recieved_invitations = Invitations::find()->where(['recipient_id'=>\Yii::$app->user->id])->all();
...
return [$recieved_invitations_count, $received_docs_count];
}
我决定,如果每个页面都需要计数,我需要触发它而不考虑控制器。所以,我在wep.php中写道:
'on beforeAction' => function ($event) {
\Yii::$app->session->set('stats', $event->sender->controller->getStats());
}
然后在布局的菜单中检索会话变量。
一切正常。但是 getStats()
操作在控制器中重复。我希望独立完成。
//action code (in '@app/components' folder) (just from documentation):
namespace app\components;
use yii\base\Action;
class HelloWorldAction extends Action {
public function run() {
return "Hello World";
}
}
//in 'actions()' in controllers:
parent::actions();
return [
'hv' => [
'class' => 'app\components\HelloWorldAction',
]
];
//in 'web.php':
'on beforeAction' => function ($event) {
\Yii::$app->session->set('stats', $event->sender->controller->hv());
}
但抛出异常:Calling unknown method: app\controllers\UsersController::hv()
。另外:如果我在配置中禁用 urlManager
。文件并注释掉 beforeAction
处理程序,可以通过此 URL:
访问操作
http://localhost:8001/index.php?r=users%2Fhv
如果在 beforeAction
事件上触发,为什么独立操作会失败?而且,如果这是正常行为,我该怎么做才能避免重复 getStats()
方法?
我实际上会尝试将 getStats 方法移动到您的用户模型,这样您就可以通过像这样调用它来从应用程序的任何地方访问它:
Yii::$app->user->identity->stats;
// OR
Yii::$app->user->identity->getStats();
拥有胖模型、简单控制器和视图通常更好。
我是 Yii 的初学者,所以不知道如何以最正确的方式解决我的问题。
有 2 个控制器 - SiteController
和 UsersController
。我需要从数据库中检索一些数据并将其输出到布局中。特别是,如果 user1 和 user2 将 user3 添加为好友,则 user3 将在主菜单面板上看到它:
/site/profile
页面,但许多其他页面(特别是 invitations
)由 [= 呈现17=]控制器)。
我在 2 个控制器中编写了相同的操作:
public function getStats () { //it duplicates in UsersController and SiteController
$recieved_invitations = Invitations::find()->where(['recipient_id'=>\Yii::$app->user->id])->all();
...
return [$recieved_invitations_count, $received_docs_count];
}
我决定,如果每个页面都需要计数,我需要触发它而不考虑控制器。所以,我在wep.php中写道:
'on beforeAction' => function ($event) {
\Yii::$app->session->set('stats', $event->sender->controller->getStats());
}
然后在布局的菜单中检索会话变量。
一切正常。但是 getStats()
操作在控制器中重复。我希望独立完成。
//action code (in '@app/components' folder) (just from documentation):
namespace app\components;
use yii\base\Action;
class HelloWorldAction extends Action {
public function run() {
return "Hello World";
}
}
//in 'actions()' in controllers:
parent::actions();
return [
'hv' => [
'class' => 'app\components\HelloWorldAction',
]
];
//in 'web.php':
'on beforeAction' => function ($event) {
\Yii::$app->session->set('stats', $event->sender->controller->hv());
}
但抛出异常:Calling unknown method: app\controllers\UsersController::hv()
。另外:如果我在配置中禁用 urlManager
。文件并注释掉 beforeAction
处理程序,可以通过此 URL:
http://localhost:8001/index.php?r=users%2Fhv
如果在 beforeAction
事件上触发,为什么独立操作会失败?而且,如果这是正常行为,我该怎么做才能避免重复 getStats()
方法?
我实际上会尝试将 getStats 方法移动到您的用户模型,这样您就可以通过像这样调用它来从应用程序的任何地方访问它:
Yii::$app->user->identity->stats;
// OR
Yii::$app->user->identity->getStats();
拥有胖模型、简单控制器和视图通常更好。