简体   繁体   English

使用FOSRestBundle的同一路线的两种方法(一种带有参数,另一种没有参数)

[英]Two methods for the same route (one with parameters, the other without) with FOSRestBundle

I'm trying to have the same route for two different functions with FOSRestBundle and Symfony. 我正在尝试使用FOSRestBundle和Symfony的两个不同功能使用相同的路由。 Here is what I have: 这是我所拥有的:

    /**
    * @Rest\Get("/users")
    * @QueryParam(name="login")
    */
    public function getLoginAvailability(ParamFetcher $paramFetcher)
    {
        return $this->_checkLoginAvailability($paramFetcher->get('login'));
    }

    /**
    * @Rest\Get("/users")
    */
    public function otherFunc()
    {
        return "other";
    }

The problem is: when I call the first function via an HTTP GET Request (giving it a login param), it is executing the second one, and ignores the first function. 问题是:当我通过HTTP GET请求(为它提供登录参数)调用第一个函数时,它正在执行第二个函数,而忽略了第一个函数。 Any advice? 有什么建议吗?

Why not handling it in one route by checking whether your param has been set and depending on the result returning what you wish? 为什么不通过检查您的参数是否已设置并根据结果返回您想要的结果来处理它呢?

/**
* @Rest\Get("/users")
* @QueryParam(name="login")
*/
public function getLoginAvailability(ParamFetcher $paramFetcher)
{

  $params = $paramFetcher->all();

  if (array_key_exists('login', $params)) {
    return $this->_checkLoginAvailability($params['login']);
  } else {
    return $this->otherFunc();
}

private function otherFunc()
{
    return "other";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM