Yii2 - denyCallback不能正常工作

时间:2016-09-27 10:10:00

标签: yii yii2

我在 web.php

中有此路线规则
'my-stokkee/buy-credits' => 'user/buycredits',

访问规则:

public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
            'access' => [
                'class' => AccessControl::className(),

                'ruleConfig' => [
                    'class' => AccessRules::className(),
                ],
                'denyCallback' => function ($rule, $action) {
                    $this->redirect('login');
                },
                'rules' => [
                    [
                        'actions' => ['signup', 'create', 'reset-password'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                    [
                        'actions' => ['index', 'view', 'update', 'changepassword', 'buyaccount', 'buycredits', 'to-usd'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                    [
                        'actions' => ['manage'],
                        'allow' => true,
                        'roles' => ['admin'],
                    ],
                ],
            ],
        ];
    }

不是重定向到登录,而是重定向到 my-stokkee / login 。知道这里发生了什么吗?

2 个答案:

答案 0 :(得分:2)

尝试使用完整路线。

return $this->redirect(['/site/login']);

答案 1 :(得分:2)

使用路线中配置的路径,例如:

return $this->redirect(['login']);

docs(强调我的)$url参数是:

  

要重定向到的网址。这可以是以下之一   格式:

     
      
  • 表示网址的字符串(例如" http://example.com")
  •   
  • 表示网址别名的字符串(例如" @ example.com")
  •   
  • 格式为[$route, ...name-value pairs...]的数组(例如['site/index', 'ref' => 1]yii\helpers\Url::to()将用于   将数组转换为URL。
  •   
     

任何相对网址都会通过在当前请求的主机信息前加上来转换为绝对网址。

相关问题