gpt4 book ai didi

symfony - 请勿将 ParamConverter 与 "fos_rest.request_body"转换器一起使用

转载 作者:行者123 更新时间:2023-12-02 13:39:44 29 4
gpt4 key购买 nike

我正在尝试使用 FOSRestBundle 的请求正文转换器,但我当前的实现似乎不起作用。

我正在使用 Symfony2、Propel、AngularJS(它将数据发送到服务器)

我做错了什么?

config.yml:

fos_rest:
routing_loader:
default_format: json
include_format: false
view:
view_response_listener: force
body_listener: true
param_fetcher_listener: true
body_converter:
enabled: true

sensio_framework_extra:
view: { annotations: false }
router: { annotations: true }
request: { converters: true }

方法:

/**
* @View()
* @Put("/documenttypes")
* @ParamConverter("documentType", converter="fos_rest.request_body")
*/
public function putAction(DocumentType $documentType)
{
print_r($documentType);
return $documentType;
}

结果我有空的模型对象:

Backend\SettingsBundle\Model\DocumentType Object
(
[id:protected] =>
[name:protected] =>
....
)

为了检查数据是否到达服务器,我修改方法:

public function putAction(DocumentType $documentType)
{
$content = $this->get("request")->getContent();
$documentType->fromArray(json_decode($content, true));

print_r($documentType);
return $documentType;
}

此方法有效并填充模型字段:

Backend\SettingsBundle\Model\DocumentType Object
(
[id:protected] => 2
[name:protected] => 'Oferta'
....
)

最佳答案

Symfony 提示您需要启用 fos_rest.converter.request_body 服务。

config.yml 内的 fos_rest 配置部分,您需要启用 body_converter 功能:

fos_rest:
body_converter:
enabled: true

现在,如果您列出服务,您会注意到服务 fos_rest.converter.request_body 已出现:

user@host:~/symfony-project$ php app/console debug:container | grep fos_rest.converter
fos_rest.converter.request_body FOS\RestBundle\Request\RequestBodyParamConverter

关于symfony - 请勿将 ParamConverter 与 "fos_rest.request_body"转换器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21512064/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com