[英]Symfony2: FOSRest: Allow route for both “/api/items” and “api/items/{id}”
So, I'm trying to create a simple REST api with Symfony2 and the FOSRestBundle. 因此,我正在尝试使用Symfony2和FOSRestBundle创建一个简单的REST api。
It's going really well, however I've hit a wall that Google apparantly hasnt been able to answer :) 进展非常顺利,但是我碰到了一堵墙,谷歌显然无法回答:)
I understand that this: 我了解这:
public function getUsersAction(){
return 'A list of all the users';
}
Will create a route as such: 将这样创建一条路线:
api_get_users GET ANY ANY /api/users.{_format}
And I know that add a function parameter: 而且我知道添加一个函数参数:
public function getUsersAction($id){
return 'A specific users (with $id) details';
}
Will create a route as such: 将这样创建一条路线:
api_get_users GET ANY ANY /api/users/{userid}.{_format}
However what if I want to make it, so both routes are available (like a proper REST api design)? 但是,如果我想这样做,那么两条路由都可用(如正确的REST API设计),该怎么办?
I tried doing something like 我尝试做类似的事情
public function getUsersAction($userid = NULL){
But it didnt work (not a huge surprise).. 但这没有用(不是很大的惊喜)。
I'm pretty sure that I'm missing a simple piece of the puzzle, but I have no idea what. 我很确定我错过了一个简单的难题,但是我不知道该怎么办。
Creating a getUserAction($id)
and a getUsersAction()
(not plural) should work, according to the documentation : 根据文档 ,创建一个getUserAction($id)
和一个getUsersAction()
(不是复数)应该可以工作:
class UserController implements ClassResourceInterface { // ... public function getUsersAction() {} // "get_users" [GET] /users public function getUserAction($slug) {} // "get_user" [GET] /users/{slug} // or when omittig "User": public function cgetAction() {} // "get_users" [GET] /users public function getAction($slug) {} // "get_user" [GET] /users/{slug} // ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.