【问题标题】:FOSRestBundle ParamFetcher POST methodFOSRestBundle ParamFetcher POST 方法
【发布时间】:2016-09-15 10:37:56
【问题描述】:

我正在使用 FOSRestBundle 创建 RESTful API。 当我使用 GET 方法通过 ajax 发送数据时,一切正常。 但我必须通过 POST 发送数据,我尝试使用 ParamFetcherListener 从 POST 获取值,但它返回空值。 当我将请求方法更改为 GET 时,它可以工作。 我做错了什么?

我的代码:

/**
 * @Rest\View(statusCode=201)
 * @QueryParam(name="test", description="test")
 */
public function createAction(Request $request, ParamFetcherInterface $paramFetcher)
{
    $test = $paramFetcher->get('test'); // it's null
}

还有 config.yml:

fos_rest:
    param_fetcher_listener: true
    body_listener:
        decoders:
            json: fos_rest.decoder.json
    view:
        view_response_listener: true
        formats:
            xml: true
            json: true
    routing_loader:
        default_format: json
    format_listener:
        rules:
            - { path: '^/api/', priorities: ['json', 'xml'], fallback_format: 'html', prefer_extension: false }    

路由:

  _wdt                       ANY      ANY      ANY    /_wdt/{token}                      
  _profiler_home             ANY      ANY      ANY    /_profiler/                        
  _profiler_search           ANY      ANY      ANY    /_profiler/search                  
  _profiler_search_bar       ANY      ANY      ANY    /_profiler/search_bar              
  _profiler_info             ANY      ANY      ANY    /_profiler/info/{about}            
  _profiler_phpinfo          ANY      ANY      ANY    /_profiler/phpinfo                 
  _profiler_search_results   ANY      ANY      ANY    /_profiler/{token}/search/results  
  _profiler                  ANY      ANY      ANY    /_profiler/{token}                 
  _profiler_router           ANY      ANY      ANY    /_profiler/{token}/router          
  _profiler_exception        ANY      ANY      ANY    /_profiler/{token}/exception       
  _profiler_exception_css    ANY      ANY      ANY    /_profiler/{token}/exception.css   
  _twig_error_test           ANY      ANY      ANY    /_error/{code}.{_format}           
  object_all                 GET      ANY      ANY    /api/objects                       
  saved_object_all           GET      ANY      ANY    /api/saved_objects                 
  saved_object_get           GET      ANY      ANY    /api/saved_objects/{id}            
  saved_object_new           POST     ANY      ANY    /api/saved_objects                 
  saved_object_delete        DELETE   ANY      ANY    /api/saved_objects/{id}            
  object_test                ANY      ANY      ANY    /api/test  

提前致谢。

【问题讨论】:

  • 方法createAction之前还有注释吗?
  • 您可以通过调用php bin/console debug:router 来查看您的应用程序中的所有路由,也请在您的问题中包含您的路由配置
  • 感谢您的回答。包括路由

标签: php api rest symfony fosrestbundle


【解决方案1】:

接受的解决方案恢复为使用标准请求对象。

如果您想在发布请求中使用 ParamFetcher,您需要使用

@RequestParam

而不是

@QueryParam

在方法的注释中。

/**
 * @Rest\View(statusCode=201)
 * @RequestParam(name="test", description="test")
 */
public function createAction(Request $request, ParamFetcherInterface $paramFetcher)
{
    $test = $paramFetcher->get('test'); // it's null
}

【讨论】:

    【解决方案2】:

    我的错误,当然POST请求中的数据是在body中发送的,body listener解码器被设置了,所以这是正确的解决方案:

    /**
     * @Rest\View(statusCode=201)
     */
    public function newAction(Request $request)
    {
        $test = $request->request->get('test'); 
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      • 2015-07-11
      • 2015-08-12
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      • 2012-05-21
      相关资源
      最近更新 更多