gpt4 book ai didi

php - 拒绝访问 Controller 后如何在 Yii2 中设置 flash 消息

转载 作者:可可西里 更新时间:2023-11-01 00:43:06 27 4
gpt4 key购买 nike

拒绝访问 Controller 后如何在 Yii2 中设置 flash 消息?这是拒绝访问的方式 http://www.yiiframework.com/doc-2.0/yii-filters-accesscontrol.html :

public function behaviors()
{
return [
'access' => [
'class' => \yii\filters\AccessControl::className(),
'only' => ['create', 'update'],
'rules' => [
// deny all POST requests
[
'allow' => false,
'verbs' => ['POST']
],
// allow authenticated users
[
'allow' => true,
'roles' => ['@'],
],
// everything else is denied
],
],
];
}

在此之后它重定向到站点/登录。我怎样才能在那里制作像“此部分仅供注册用户使用”之类的即时消息?

最佳答案

您可以尝试为拒绝添加回调吗?我希望这有效:

public function behaviors()
{
return [
'access' => [
'class' => \yii\filters\AccessControl::className(),
'only' => ['create', 'update'],
'rules' => [
// deny all POST requests
[
'allow' => false,
'verbs' => ['POST']
],
// allow authenticated users
[
'allow' => true,
'roles' => ['@'],
],
// everything else is denied
],
'denyCallback' => function ($rule, $action) {
Yii::$app->session->setFlash('error', 'This section is only for registered users.');
Yii::$app->user->loginRequired();
},
],
];
}

关于php - 拒绝访问 Controller 后如何在 Yii2 中设置 flash 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28398421/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com