禁止(#403) - 您不被允许执行此操作[Yii2]

时间:2015-09-08 02:35:36

标签: yii2

我尝试在后端添加菜单map。我使用yii2-advanced。这是我的“控制器”代码:

public function actionMap()
{
    return $this->render('map');
}

但是,当我尝试使用此网址http://localhost/yii2advanced/backend/web/index.php?r=site/map访问它时,我收到了错误消息Forbidden (#403) - You are not allowed to perform this action。我不明白为什么我收到此错误消息,有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:7)

AccessControl引起的。最有可能根据访问规则阻止操作map。允许所有经过身份验证的用户的示例:

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

或者,您可以根据某些RBAC角色调整访问权限。

答案 1 :(得分:0)

除了arogachev的回答: 将其粘贴到站点控制器中:

   public function behaviors() {
    return [
        'access' => [
            'class' => AccessControl::className(),
            'rules' => [
                [
                    'actions' => ['login', 'error'],
                    'allow' => true,
                ],
                [
                    'actions' => ['logout', 'index'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
                [
                    'allow' => true,
                    'roles' => ['@'],
                ],
            ],
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'logout' => ['post'],
            ],
        ],
    ];
}