这是后端SiteController.php
访问规则。当我浏览此网址site.com/backend/web/site/login
时。它显示禁止(#403)。
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error'],
'allow' => true,
],
[
'actions' => ['logout', 'index', 'addhotels'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
答案 0 :(得分:4)
处理另一个错误时发生错误:异常 '警予\网络\ ForbiddenHttpException'有消息'你不被允许 执行此操作。'在 C:\瓦帕\ WWW \ķ\ kometonline \厂商\ yiisoft \ yii2 \滤波器\ AccessControl.php:151
我也收到了这个错误,并通过Google找到了这个页面,所以希望这会有助于其他人。
错误的发生是因为您已添加了访问控制,但您还需要明确允许“错误”错误。站点控制器中的操作,否则您将得到相同的错误。它没有立即显而易见,因为它没有针对它的行动,也添加了“验证码”'行动,或者你会遇到同样的问题。
在您的站点控制器中:
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['register','login'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
[
//see captcha and error added here, this fixes the issue
'actions' => ['contact', 'about', 'terms', 'forgot', 'reset-password', 'captcha', 'error'],
'allow' => true,
'roles' => ['?', '@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
答案 1 :(得分:0)
您需要从login
列表中删除AccessControl
操作。或在?
中为访客用户添加AccessControl
作为角色。
例如,
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error'],
'allow' => true,
'roles' => ['?'], // " ? " for guest user
],
[
'actions' => ['logout', 'index', 'addhotels'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
答案 2 :(得分:0)
我还想知道如何允许未登录的用户在后端没有收到Forbidden错误。我只是尝试使用单个
渲染一个测试视图<h1>Test</h1>
我收到了Forbidden错误。
答案 3 :(得分:0)
也许您在尝试访问登录页面时已经以用户身份登录。这将抛出ForbiddenHttpException。或者,您可以通过配置denyCallback属性来自定义此行为:
[
'class' => AccessControl::className(),
'rules' => [...],
'denyCallback' => function ($rule, $action) {
//Add your error handler here
throw new \Exception('You are not allowed to access this page');
}
]