Symfony使用带有POST动作的参数转换器



我正在构建rest API,并有保存帖子的方法。

postPostAction(Request $request)
{
}

我的POST请求包含所有Entity/Post属性

如何使用ParamConverter使Entity/Post在此方法参数如下:

postPostAction(Post $post)
{
}

我需要创建自定义ParamConverter,或者使用参数转换器仅与PUTGET有意义吗?

您可以像这样使用默认的参数转换器

use SensioBundleFrameworkExtraBundleConfigurationParamConverter;
/**
 * @ParamConverter("thePost", class = "AppBundle:Post")
 */
public function postPostAction(Post $thePost)
{
    // ...
}

注释接受几个参数,其中包括要转换的变量名和要转换到的类。

你甚至可以省略注释而写

public function postPostAction(Post $thePost)
{
    // ...
}

这将自动将变量转换为实体。

您可以在文档中找到更多信息。

最新更新