Php Yii2 Rest api put方法返回内部服务器错误-由于未知原因无法更新对象
我正在用yii2创建一个API,除了Php Yii2 Rest api put方法返回内部服务器错误-由于未知原因无法更新对象,php,rest,api,yii,yii2,Php,Rest,Api,Yii,Yii2,我正在用yii2创建一个API,除了put方法之外,所有方法都可以正常工作。在保存之前,我在方法中获取数据,但在保存时得到以下错误 {“名称”:“内部服务器错误”,“消息”:“更新失败” 对象因未知原因被删除。“,“代码”:0,“状态”:500, “类型”:“yii\web\ServerErrorHttpException”} 这是我的控制器文件 ProductsController.php <?php namespace app\controllers; use yii\rest\A
put
方法之外,所有方法都可以正常工作。在保存之前,我在方法中获取数据,但在保存时得到以下错误
{“名称”:“内部服务器错误”,“消息”:“更新失败”
对象因未知原因被删除。“,“代码”:0,“状态”:500,
“类型”:“yii\web\ServerErrorHttpException”}
这是我的控制器文件
ProductsController.php
<?php
namespace app\controllers;
use yii\rest\ActiveController;
use yii\filters\auth\HttpBearerAuth;
class ProductsController extends ActiveController {
public $modelClass = 'app\models\Product';
public function __construct($id, $module, $config = array()) {
parent::__construct($id, $module, $config);
}
public function behaviors() {
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => HttpBearerAuth::className(),
];
return $behaviors;
}
}
我最近也遇到了同样的问题,解决办法是数据传递不正确。通常它应该作为json传递
namespace app\controllers;
use yii\rest\ActiveController;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\VerbFilter;
use yii\web\Response;
use yii\helpers\ArrayHelper;
class ProductsController extends ActiveController {
public $modelClass = 'app\models\Product';
public function __construct($id, $module, $config = array()) {
parent::__construct($id, $module, $config);
}
public function behaviors() {
return ArrayHelper::merge(parent::behaviors(), [
[
'class' => HttpBearerAuth::className(),
'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']
],
]
]);
}
尝试在json
{
"name": "product name"
}
namespace app\controllers;
use yii\rest\ActiveController;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\VerbFilter;
use yii\web\Response;
use yii\helpers\ArrayHelper;
class ProductsController extends ActiveController {
public $modelClass = 'app\models\Product';
public function __construct($id, $module, $config = array()) {
parent::__construct($id, $module, $config);
}
public function behaviors() {
return ArrayHelper::merge(parent::behaviors(), [
[
'class' => HttpBearerAuth::className(),
'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']
],
]
]);
}
{
"name": "product name"
}