我正在使用Symfony和FOSRestBundle創建一個REST API,而且這兩者都非常新穎。FOSRestBundle,生成創建資源的URL
現在我想知道如何生成我剛剛創建的資源的URL。路線都設置這樣的:
# app/config/routing.yml
characters:
type: rest
prefix: /api
resource: "@Optc/Resources/config/routing/characters_routing.yml"
NelmioApiDocBundle:
prefix: /api/doc
resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
# Resources/Optc/Resources/config/routing/characters_routing.yml
characters:
type: rest
resource: Optc\Controller\CharactersController
人物控制器,創建資源的一部分:
$character = new Character();
$form = $this->createForm(new CharacterType(), $character);
$form->bind($data);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($character);
$em->flush();
$response->headers->set('Location', $this->generateUrl('get_characters', array('id' => $user->getId()), true));
$view = $this->view($character, 200);
return $this->handleView($view);
}
更新:全控制器代碼:
<?php
namespace Optc\Controller;
use FOS\RestBundle\Controller\Annotations\QueryParam;
use FOS\RestBundle\Controller\FOSRestController;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Optc\Entity\Character;
use Optc\Form\CharacterType;
use Optc\HttpFoundation\File\Base64File;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
/**
* Characters Controller
*/
class CharactersController extends FOSRestController
{
/**
* Get the list of characters.
*
* @param string $page integer with the page number (requires param_fetcher_listener: force)
*
* @return array data
*
* @QueryParam(name="page", requirements="\d+", default="1", description="Page number of the overview.")
* @ApiDoc()
*/
public function getCharactersAction($page)
{
$characters = $this
->getDoctrine()
->getRepository('Optc:Character')
->findAll();
$view = $this->view($characters, 200);
return $this->handleView($view);
}
public function getCharacterAction($id)
{
$character = $this
->getDoctrine()
->getRepository('Optc:Character')
->findOneById($id);
if (!$character) {
throw new HttpException(404, sprintf('Character with id %d not found!', $id));
}
$view = $this->view($character, 200);
return $this->handleView($view);
}
/**
* Create a new character.
*
* @param Request $request
*
* @return View view instance
*
* @ApiDoc()
*/
public function postCharacterAction(Request $request)
{
$data = $request->request->all();
// If the request contains image date, first convert it from its base64 enconding to a real file
if ($request->request->has('image') && $request->request->get('id')) {
$imagePath = realpath($this->get('kernel')->getRootDir() . '/../web'.$this->container->getParameter('upload_path_characters')).'/'.$request->request->get('id');
$file = Base64File::create($imagePath, $request->request->get('image'));
$data['image'] = $file;
}
$character = new Character();
$form = $this->createForm(new CharacterType(), $character);
$form->bind($data);
var_dump($form->isValid());
var_dump($form->getErrorsAsString());
var_dump($this->generateUrl('get_characters', array('id' => $character->getId()), true));
die();
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($character);
$em->flush();
$response->headers->set('Location', $this->generateUrl('acme_demo_user_get', array('id' => $user->getId()), true));
$view = $this->view($character, 200);
return $this->handleView($view);
}
else {
}
}
}
的不像我預期的那樣工作的是generateUrl
部分設置位置標題。它吐出http://optc.local/api/characters?id = 2。這當然只會列出全部資源。但我想要的是http://optc.local/api/characters/2。
我該怎麼做?似乎我錯過了一些簡單的東西。
(順便說一句,關於返回位置頭是從http://williamdurand.fr/2012/08/02/rest-apis-with-symfony2-the-right-way/ PHP的一部分,所以我希望這是「正確」的方式。)
PLZ轉儲控制器類這裏的路線
,它似乎'的getAction ()方法沒有'id'參數,當你將額外**參數**傳遞給路由時,它們轉換爲查詢參數 – ghanbari
@ghanbari添加了完整的控制器代碼。它有一個'id'參數,但我不確定這是否足夠。 – maryisdead
請轉儲路由:console debug:router,我認爲你必須使用'get_character'路由 – ghanbari