【发布时间】: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