【问题标题】:Two methods for the same route (one with parameters, the other without) with FOSRestBundle使用 FOSRestBundle 用于同一路由的两种方法(一种带参数,另一种不带参数)
【发布时间】:2019-01-10 08:18:40
【问题描述】:

我正在尝试使用 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 请求(给它一个登录参数)调用第一个函数时,它正在执行第二个函数,而忽略了第一个函数。有什么建议吗?

【问题讨论】:

    标签: php rest symfony fosrestbundle


    【解决方案1】:

    为什么不通过检查您的参数是否已设置并根据返回的结果在一个路由中处理它?

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

    【讨论】:

      猜你喜欢
      • 2012-11-06
      • 2011-12-10
      • 1970-01-01
      • 2015-08-14
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      相关资源
      最近更新 更多