使用形式编码的点路径属性创建对象



在laravel/dingo中,您可以通过将数据发布为 application/x-www-form-urlencodedapplication/json

来创建新对象
$ curl -XPOST --data name=foo http://example.org/user

json:

$ curl -XPOST -H 'Content-type: application/json' --data '{"name":"foo"}' http://example.org/user

两个工作正常。


我现在想做的就是创建具有嵌套属性的对象,例如name.first。发布JSON时,这正常工作:

$ curl -XPOST -H 'Content-type: application/json' --data '{"name":{"first:"foo"}}' http://example.org/user

,但使用表单编码的数据时会失败:

$ curl -XPOST --data name.first=foo http://example.org/user

例外是The name.first field is required


我知道PHP将点转换为下划线:

php将自动替换传入变量名称中的任何点。

这可能是Laravel未检测到变量被嵌套的原因。


如何使Laravel正确地检测到可变名称中的点路径?

请使用

$ curl -XPOST --data name%5Bfirst%5D=foo http://example.org/user

等于name['first'] = foo

:)

最新更新