如何在 Yii2 上处理 Vue/Axios Json 有效负载发布的数据负载、有效、数据、如何在

2023-09-07 10:37:07 作者:你还欠我一个拥抱

我花了一段时间才明白这一点,因为它有点明显.我会回答自己,所以其他人可以从答案中受益,当然看看是否有更好的方法来做到这一点.该问题基于 Axios/Yii2,但我想这同样适用于其他前端库/框架向 Yii2 发送数据.

It took me a while to understand this, being that it was a little obvious. I will answer myself, so other can benefit of the answer and ofcourse to see if there's a better way to do this. The problem was based on Axios/Yii2 but I guess this will apply equally to other frontend libraries/frameworks sending data to Yii2.

我需要从 Vuejs 制作的小表单中发布数据,将请求 Axios 发送到 Yii2 上的 Action/Controller,因此数据通过简单的 POST 请求发送,并且帖子正在到达控制器,但我没有能够接收关于动作的数据,$_POST |$post 为空(使用 xdebug 检查).

I needed to post data from a small form made on Vuejs, sending the request Axios to a Action/Controller on Yii2, so data is sent on a simple POST request and the post is getting to the controller, but I was not able to receive the data on the action, $_POST | $post arrives empty (checked using xdebug).

据我所知,这与安全有关.但我已经尝试禁用 public $enableCsrfValidation,所以这不是问题.

As much as I remember, this had something to do with security. But I already tried by disabling public $enableCsrfValidation, so that was not the problem.

public $enableCsrfValidation = false;

但无论如何,Yii2 内部的请求/发布数据中并没有添加数据.

But no matter what, data was not being added to the request/post data inside Yii2.

下图解释了你会发现的问题:

The following Image, explains the problem you will find there:

发送带有测试数据的帖子的 Axisos 方法.Yii2 Action 停在了那个地方,应该可以看到数据了.为请求捕获 xdebug 变量和数据.您可以在其中检查有效负载的 Chrome 捕获已发送.

推荐答案

答案正如我所说的有点明显",但我看不到这一点,我相信其他一些开发人员可能会对此表示赞同.

The answer is as I said "kind of obvious", but I could not see that, and I am sure some other devs will probably fall on this.

在疯狂搜索并询问所有人之后,我尝试使用 Postman 应用程序发送请求,是的,我知道测试 api 的最佳方法.

After searching like crazy and asking everyone, I tried sending the request by using Postman app, yup the best thing I know to test apis.

不要忘记添加 xdebug cookie 以便能够调试您的 PHP 端点.

Dont forgue to add the xdebug cookie to be able to debug your PHP Endpoint.

在那里我找到了第一个线索«明显的部分»,我没有将数据作为表单数据发送,Axios 和其他库将数据作为原始(应用程序/json)有效负载发送.

There I found the first clue «the obvious part», I was not sending data as a form-data, Axios and other libraries, send the data as a raw (application/json) payload.

这意味着 Yii2 将无法在 post 请求中找到数据,是的,它在那里,但 Yii2 的魔法不起作用,你也不会在 $GLOBALS 或 $_POST 中找到这些数据.

This means that Yii2 will no be able to find the data inside the post request, yes its there but Yii2 magic will not work, neither you will find this data inside $GLOBALS or in $_POST.

阅读 Yii2 文档后,我发现在请求内部我可以使用一个函数来帮助我恢复原始数据,因此请使用以下行:

So reading the Yii2 documentation I found that inside request I can use a function that will help me recovering the Raw data, so to do this use the following line:

$raw_data = Yii::$app->request->getRawBody();

现在,这些数据以简单的原始 json 字符串形式提供给您,因此请使用 PHP 的强大功能将其解析为对象.

Now, that data gets to you as a simple, raw json string, so use the power of PHP to parse it to an object.

$object= json_decode($raw_data );

最后通过调用你寻找的属性来使用里面的数据,在负载上发送:

And finally use the data inside by calling the properties you look for, sent on the pay load:

Json 有效负载:

{
    "msg":"This is my payload",
    "id":"11"
}

使用它:

echo $object->{'msg'}; // prints: This is my payload

所以这就是处理这个问题的方法,现在我想了解其他一些观点,看看是否有更好的方法或更清洁的方法来做到这一点.希望对您有所帮助.

So that's the way to handle that, now I would like some other points of view to see if there's a better way or cleaner way to do this. Hope it helps.