yii2 我只想允许索引操作
yii2 I want to allow only index action
我尝试使用这个 behaviors() 函数只允许索引操作
现在它也被拒绝索引
任何人都可以解决并解释规则return。
<?php
namespace api\modules\v1\controllers;
use yii\rest\ActiveController;
use yii\filters\AccessControl;
/**
* Doctor Controller API
*/
class DoctorController extends ActiveController
{
public $modelClass = 'api\modules\v1\models\Doctor';
public function behaviors()
{
return [
'access' => [
'class' => \yii\filters\AccessControl::className(),
'only' => ['index'],
'rules' => [
[
'allow' => false,
'verbs' => ['POST']
],
[
'allow' => true,
'actions' => ['index'],
'verbs' => ['GET'],
'roles' => ['?'],
],
[
'allow' => true,
'roles' => ['?'],
],
// everything else is denied
],
],
];
}
}
看了文档我明白了
通过覆盖原始访问行为,它将决定任何未提及的内容都是允许的。
因此,您只需要一个允许动作的规则,而无需使用动词或角色,但如果您想对其进行更多限制,则可以。
<?php
namespace api\modules\v1\controllers;
use Yii;
use api\modules\v1\models\Doctor;
use yii\rest\ActiveController;
/**
* Doctor Controller API
*/
class DoctorController extends ActiveController
{
public $modelClass = 'api\modules\v1\models\Doctor';
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['access'] = [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
[
// All actions
'allow' => true,
'actions' => ['index'],
],
],
];
return $behaviors;
}
}
我尝试使用这个 behaviors() 函数只允许索引操作 现在它也被拒绝索引
任何人都可以解决并解释规则return。
<?php
namespace api\modules\v1\controllers;
use yii\rest\ActiveController;
use yii\filters\AccessControl;
/**
* Doctor Controller API
*/
class DoctorController extends ActiveController
{
public $modelClass = 'api\modules\v1\models\Doctor';
public function behaviors()
{
return [
'access' => [
'class' => \yii\filters\AccessControl::className(),
'only' => ['index'],
'rules' => [
[
'allow' => false,
'verbs' => ['POST']
],
[
'allow' => true,
'actions' => ['index'],
'verbs' => ['GET'],
'roles' => ['?'],
],
[
'allow' => true,
'roles' => ['?'],
],
// everything else is denied
],
],
];
}
}
看了文档我明白了
通过覆盖原始访问行为,它将决定任何未提及的内容都是允许的。
因此,您只需要一个允许动作的规则,而无需使用动词或角色,但如果您想对其进行更多限制,则可以。
<?php
namespace api\modules\v1\controllers;
use Yii;
use api\modules\v1\models\Doctor;
use yii\rest\ActiveController;
/**
* Doctor Controller API
*/
class DoctorController extends ActiveController
{
public $modelClass = 'api\modules\v1\models\Doctor';
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['access'] = [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
[
// All actions
'allow' => true,
'actions' => ['index'],
],
],
];
return $behaviors;
}
}