简体   繁体   中英

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. 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. 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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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