FOSRestBundle ParamFetcher POST method


FOSRestBundle ParamFetcher POST method

我正在使用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  

提前谢谢。

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

如果你想在发布请求时使用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
}

我的错误,POST请求中的数据当然是在主体中发送的,主体侦听器解码器是设置好的,所以这是正确的解决方案:

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