如何处理 Vue/Axios Json payload 在 Yii2 上发布的数据 [英] How to process Vue/Axios Json payload posted data on Yii2

查看:25
本文介绍了如何处理 Vue/Axios Json payload 在 Yii2 上发布的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一段时间才理解这一点,因为这有点明显.我会回答自己,所以其他人可以从答案中受益,当然还有看看是否有更好的方法来做到这一点.该问题基于 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).

据我所知,这与安全有关.但我已经尝试禁用公共 $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:

  1. 发送带有测试数据的帖子的 Axisos 方法.
  2. Yii2 Action停在那个地方,我应该可以看到数据.
  3. 为请求捕获 xdebug 变量和数据.
  4. 您可以在其中检查负载的 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 );

最后通过调用你要查找的属性来使用里面的数据,在payload上发送:

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.

这篇关于如何处理 Vue/Axios Json payload 在 Yii2 上发布的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆