繁体   English   中英

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

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

我正在尝试使用FOSRestBundle和Symfony的两个不同功能使用相同的路由。 这是我所拥有的:

    /**
    * @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";
    }

问题是:当我通过HTTP GET请求(为它提供登录参数)调用第一个函数时,它正在执行第二个函数,而忽略了第一个函数。 有什么建议吗?

为什么不通过检查您的参数是否已设置并根据结果返回您想要的结果来处理它呢?

/**
* @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