首页 > 解决方案 > 在 api 平台上调用带有 json 数据的控制器

问题描述

我已经使用 make:entity 构建了我的应用程序,

在我创建一个链接所有表以获得我想要的东西的查询之后(所有表的一点)

我的查询正在运行,但现在我想使用 API 平台来处理路由。

但我找不到用 API 平台表示法调用我的控制器的方法。

关于 Caractéristiques 实体的注释。

    #[ApiResource(collectionOperations: ['requete' => [
        'method'          => 'get',
        'path'            => '/requete',
        'controller'      => ApiRequeteController::class,
        'openapi_context' => [
            'summary'     => 'get requete',
            'requestBody' => [
                'content' => [
                    'application/json' => [
                        'schema'  => [
                            'type'       => 'object',
                            'properties' =>
                                [
                                    'montant' => ['type' => 'int'],
                                    'loc' => ['type' => 'string'],
                                    'stat' => ['type' => 'string'],
                                    'type' => ['type' => 'string'],
                                    'sect' => ['type' => 'string'],
                                ],
                        ],
                        'example' => [
                            'montant' => 1000000,
                            'loc' => "test",
                             'stat' => "test",
                             'type' => "test",
                             'sect' => "test",
                        ],
                    ],
                ],
            ],
        ],
    ],
])]

我找到了这种方式,但不起作用。

给我吗:

语法错误,意外 '=>' (T_DOUBLE_ARROW)

'方法' => '得到',

有什么办法可以做到这一点?

标签: apisymfony

解决方案


使用 POST 和另一种写作

* collectionOperations= {"requete" = {
 *       "method"          = "POST",
 *       "path"            = "/requete",
 *       "controller"      = App\Controller\ApiRequeteController::class,
 *       "openapi_context" = {
 *          "summary"     = "get requete",
 *           "requestBody" = {
 *               "content" = {
 *                   "application/json" = {
 *                       "schema"  = {
 *                           "type"       = "object",
 *                           "properties" =
 *                               {
 *                                   "montant" = {"type" = "int"},
 *                                   "loc" = {"type" = "string"},
 *                                   "stat" = {"type" = "string"},
 *                                   "type" = {"type" = "string"},
 *                                   "sect" = {"type" = "string"},
 *                               },
 *                       },
 *                       "example" = {
 *                           "montant" = 1000000,
 *                           "loc" = "test",
 *                            "stat" = "test",
 *                            "type" = "test",
 *                            "sect" = "test",
 *                       },
 *                   },
 *               },
 *           },
 *       }
 *   }
 * }
 * )
 */

推荐阅读