Yii2请求PUT无法正常工作
我在yii2中使用休息API Authorization : Bearer
和我的update
操作需要使用PUT
发送数据。我已经完全配置了actionUpdate
,但不知何故,我在Request PUT
中没有收到任何数据。Yii2请求PUT无法正常工作
我在网上发现了几篇关于Yii2 PUT
问题的文章,但是找不到天气有没有解决方案呢?
一个文章或发行的是github issue并将其指向这个github issue
广告,如果没有办法解决比我应该用什么替代的Update
行动呢。
这里是我的actionUpdate
代码
public function actionUpdate($id)
{
$params = Yii::$app->request->bodyParams;
$model = Event::find()->where(['event_id'=>$id])->andWhere(['partner_id' => Yii::$app->user->id])->one();
if($model !== null){
$model->load($params, '');
$model->partner_id = Yii::$app->user->id;
$model->updated_date = time();
if ($model->save()) {
$this->setHeader(200);
echo json_encode(array('status'=>1,'data'=>array_filter($model->attributes)),JSON_PRETTY_PRINT);
}
}
}
这是调试屏幕的截图。请参阅event_name
属性。
这是$model->load($params,'')
线的执行之后的屏幕截图。
我打电话的服务如下,不能Update
正确的数据。我的服务通过邮递员工作正常。所以我想我在CURL
请求中缺少一些东西。
$service_url = 'http://localhost/site-api/api/web/v1/events/'.$eventDetailDBI->gv ('id');
$curl = curl_init($service_url);
$curl_post_data = array(
"event_name" => $eventDetailDBI->gv ('name'),
);
$header = array();
$header[] = 'Authorization: Bearer 4p9mj82PTl1BWSya7bfpU_Nm';
$header[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,$header);
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
$json = json_decode($curl_response, true);
curl_close($curl);
我在我POST
领域得到正确的数据,传递正确的数据,但该服务犯规更新任何数据。
谢谢
试试这个:
public function actionUpdate($id)
{
// this will get what you did send as application/x-www-form-urlencoded params
// note that if you are sending data as query params you can use Yii::$app->request->queryParams instead.
$params = Yii::$app->request->bodyParams;
$model = Event::find()->where(['event_id'=>$id])->andWhere(['partner_id' => Yii::$app->user->id])->one();
if($model !== null){
// This will load data to your safe attribute as defined in your model rules using your default scenario.
$model->load($params, '');
$model->partner_id = Yii::$app->user->id;
$model->updated_date = time();
if ($model->save()) {
/*
you can use Yii::$app->getResponse()->setStatusCode(200) here but no need to do that.
response will be 200 by default as you are returning data.
*/
// yii\rest\Serializer will take care here of encoding model's related attributes.
return [
'status' => 1,
'data' => $model
];
}
else {
// when validation fails. you model instance will hold error messages and response will be auto set to 422.
return $model;
}
}
}
它的工作方式。我确实看到了'$ params'中的参数,但它也包含许多其他数据,所以我不知道如何只在它中加载模型属性。 '$ params'具有其他值,如'WebKitFormBoundary'等 –
那是什么[load()](http://www.yiiframework.com/doc-2.0/yii-base-model.html#load() - 详情)。它只会从params得到你的model :: rules()认为是安全的属性。换句话说,如果'WebKitFormBoundary'没有在你的模型规则()中定义,或者至少没有设置为安全的,那么在将数据加载到你的模型时它将被忽略。有关更多详细信息,请参阅[Yii文档](http://www.yiiframework.com/doc-2.0/guide-structure-models.html#validation-rules)。 –
我从你的评论上面那行代码中得到了。但它没有加载数据。我正在上传调试截图。我现在正在向邮递员提出要求。我知道这不重要,但只要你知道它是否会影响。 –
请邮寄样本代码。谢谢 –
哪些问题?我不知道他们的github回购中有任何开放的PUT相关问题。我没有得到PUT工作的唯一时间是因为我的IIS服务器默认情况下禁用了PUT动词,并且与Yii无关。你能否通过分享代码或链接任何这些文章来指定PUT问题? –