FOSRestBundle,生成创建资源的URL

时间:2015-11-03 09:02:34

标签: php api rest symfony fosrestbundle

我正在使用Symfony和FOSRestBundle创建REST API,而且我对这两者都很陌生。

现在我想知道如何生成我刚刚创建的资源的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 {

        }
    }
}

不能像我预期的那样工作的是设置 Location 标头的generateUrl部分。它吐出http://optc.local/api/characters ?id = 2 。这当然只会列出所有资源。但我想要的是http://optc.local/api/characters / 2

我该怎么做?好像我错过了一些简单的东西。

(顺便说一句,关于返回 Location 标题的PHP部分来自http://williamdurand.fr/2012/08/02/rest-apis-with-symfony2-the-right-way/,所以我希望这是&#34;正确的方式。)< / p>

2 个答案:

答案 0 :(得分:1)

您必须使用get_character路线代替get_characters路线

我建议您实施ClassResourceInterface或使用RouteResource注释,可以使用方法名称getActioncgetAction(这只是建议)

答案 1 :(得分:1)

你应该检查终端中的app/console debug:router以查看symfony命名路由的名称

在我的情况下,它使用了减号而不是下划线

get-character