【问题标题】:passing parameters to get function within FOSRestBundle在 FOSRestBundle 中传递参数以获取函数
【发布时间】:2015-11-22 09:22:17
【问题描述】:

简而言之,在访问 GET 操作时,还需要结束诸如 offset/limit 之类的操作。我正在徘徊如何为此设置参数?

像这样:

public function getThreadsAction($userKey, $offset, $limit)
{ #logic here
  ......
}

或者使用ParameterFetcherInterface:

public function getThreadsAction($userKey, ParameterFetcherInterface $fetcher)
{ #logic here
  $offset = $fetcher->get('offset');
  ......
}

或者直接使用Request:

public function getThreadsAction($userKey, Request $request)
{ #logic here
  $offset = $request->get('offset');
  ......
}

暂时如果它像第一个,路线看起来像/api/threads/{userKey}.{_format}
我需要设置@GET()annotation 吗?否则,如果它以其他方式进行,是否有必要为limitoffset设置@QueryParam

提前致谢。

【问题讨论】:

    标签: rest symfony fosrestbundle


    【解决方案1】:

    我总是使用查询参数来限制、偏移、搜索(和其他过滤器)来寻找类似的东西。所以你可以像这样访问它:

    $request->query->get('limit')
    

    另外,最好在配置中设置默认限制值,因此如果客户端不发送限制值,您将使用配置中的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-17
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-02
      • 2018-07-06
      • 2010-10-02
      相关资源
      最近更新 更多