在我的控制器中:
namespace app\api\modules\v1\controllers;
use yii\rest\ActiveController;
use yii\filters\VerbFilter;
use yii\web\Response;
class CountryController extends ActiveController
{
public $modelClass = 'app\models\Country';
public function behaviors()
{
return [
[
'class' => 'yii\filters\ContentNegotiator',
'only' => ['index', 'view','create','update','search'],
'formats' => ['application/json' =>Response::FORMAT_JSON,],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'index'=>['get'],
'view'=>['get'],
'create'=>['post'],
'update'=>['PUT'],
'delete' => ['delete'],
'deleteall'=>['post'],
'search' => ['get']
],
]
];
}
}`
我尝试使用我的POSTMAN应用程序
对于创建我使用POST http://localhost/myapp/api/v1/countries正常工作。但是对于更新我使用PUT http://localhost/myapp/api/v1/countries/16它返回16记录,因为JSON输出未按预期更新。
出了什么问题?谢谢!
答案 0 :(得分:5)
答案 1 :(得分:0)
如果您觉得使用它,可以选择其他选项。而不是behaviors()
你可以添加这样的东西,它将起到同样的作用,你不会有任何问题。
public function actions()
{
$actions = parent::actions();
unset($actions['index']);
unset($actions['create']);
unset($actions['delete']);
unset($actions['update']);
unset($actions['view']);
return $actions;
}