FOSRestBundle为我的路线

时间:2015-05-08 18:56:44

标签: php symfony fosrestbundle

我在我的项目中安装FOSRestBundle并进行配置:

fos_rest:
    param_fetcher_listener: true
    body_listener: true
    format_listener: true
    view:
        view_response_listener: 'force'
        formats:
            xml: true
            json : true
        templating_formats:
            html: true
        force_redirects:
            html: true
        failed_validation: HTTP_BAD_REQUEST
        default_engine: twig
    routing_loader:
        default_format: json

在我的routing.yml:

api:
   resource: routing_api.yml
   prefix: /api

routing_api.yml

books:
  type: rest
  resource: @ApiBundle/Controller/BookController.php

和BookController:

   //namespaces..
    /**
     * Class BookController
     * @package ApiBundle\Controller
     */
    class BookController extends BaseApiController
    {
        public function getBookAction($id)
        {
            $book = $this->getDoctrine()->getManager()->getRepository('SiteBundle:Books\Books')->find($id);
            if(!$book)
            {
                throw new NotFoundHttpException('Book not found');
            }

            return $book;

        }



    }

接下来我运行控制台命令: $ php app/console route:debug | grep api 得到:

get_book_book GET ANY ANY /api/books/{id}/book.{_format} 所以为什么? 如何正确配置,所以我这样:/api/books/{id}.{_format}

1 个答案:

答案 0 :(得分:1)

试试这个:

// routing_api.yml
books:
    type:     rest
    resource: @ApiBundle/Controller/BookController.php

get_book_book:
    pattern:  /books/{id}.{_format}
    defaults: { _controller: ApiBundle:Book:myAction, _format: json }