Reputation:
I'm install FOSRestBundle in my project and configure it:
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
In my routing.yml:
api:
resource: routing_api.yml
prefix: /api
routing_api.yml
books:
type: rest
resource: @ApiBundle/Controller/BookController.php
And 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;
}
}
Next i'm run console command:
$ php app/console route:debug | grep api
And get:
get_book_book GET ANY ANY /api/books/{id}/book.{_format}
So, why?
How to configure it properly, so I got like this: /api/books/{id}.{_format}
Upvotes: 2
Views: 810
Reputation: 271
Try this:
// 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 }
Upvotes: 1