API平台中的多种操作方法

问题描述 投票:1回答:2

我正在写一个自定义动作,我需要使它可用于GET(集合)和POST方法。

我的注释看起来像这样。

/**
 * @Route(
 *     name="api_entity_custom",
 *     path="/entity/custom",
 *     defaults={
 *      "_api_resource_class"=Entity::class,
 *      "_api_collection_operation_name"="EntityCustom"
 *     }
 * )
 * @Method("GET")
 */

这适用于GET,但是当我添加POST时,我只在文档中看到GET(swagger)

@Method({"GET", "POST"})

如果我改变顺序,那么我看到POST但不是GET

@Method({"POST", "GET"})

是否有可能做到这一点?怎么样?

编辑:我正在使用这样的结构

//Path/To/Entity/Action/EntityCustomAction.php  
class EntityCustomAction
    {
        /**
         * @Route(
         *     name="api_entity_custom",
         *     path="/entity/custom",
         *     defaults={
         *      "_api_resource_class"=Entity::class,
         *      "_api_collection_operation_name"="EntityCustom"
         *     }
         * )
         * @Method("GET")
         */

        public function __invoke($data)
{
...

在路由中启用

entity:
   resource: '@EntityBundle/Action/'
   type:     'annotation'
api-platform.com
2个回答
0
投票

您可以通过以下方式执行此操作:

Action 1:

/**
 * @Route("/data/save", name="data_save")
 * @Method({"GET"})
 * @Template()
 */
public function dataSaveViewAction()
{
    // code here...
}

Action 2:

/**
 * @Route("/data/save", name="data_save")
 * @Method({"POST"})
 */
public function dataSaveAction(Request $request)
{
    // code here ...
}


0
投票

这里的问题是您对两个操作使用相同的名称。但是,路径的名称必须是唯一的。如果没有,API-Platform将不会显示它。你应该做这样的事情:

    Action 1:

/**
 * @Route("/data/save", name="data_save_get") // Choose a unique name
 * @Method({"GET"})
 * @Template()
 */
public function dataSaveViewAction()
{
    // code here...
}

Action 2:

/**
 * @Route("/data/save", name="data_save_post") // same here
 * @Method({"POST"})
 */
public function dataSaveAction(Request $request)
{
    // code here ...
}
© www.soinside.com 2019 - 2024. All rights reserved.