Yii2 请求 PUT 无法正常工作

Yii2 request PUT not working properly

我在 yii2 中使用 rest api 和 Authorization : Bearer,我的 update 操作需要使用 PUT 发送数据。我已经完全配置了 actionUpdate 但不知何故我没有在 Request PUT.

中获取任何数据

我在网上发现了一些关于 Yii2 PUT 问题的文章,但找不到天气是否有任何解决方案?

其中一篇文章或问题是 github issue and it points to this 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 数据。我的服务通过 postman.So 工作正常,我想我在 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;
        }
    }
 }