sulu / ExampleNewsBundle

Sulu news example bundle
5 stars 3 forks source link

FOSRestBundle automatic pluralization #22

Open abitofBaileys opened 6 years ago

abitofBaileys commented 6 years ago
Q A
Bug? No
Feature? Unfortunately

Problem

In this ExampleNewsBundle a potential issue doesn't occur due to the nature of "news" being plural by default. Should someone try to recreate the steps to create his own bundle based on the pull requests or the blog posts, they may run into Route not found problems regarding the routing as FOSRestBundle defaults routes to plural.

Example

Creating a Bundle working with singular CarBundle results in different routes if you work with singular terms throughout the tutorial, ultimately ending up with singular and plural routes conflicting.

get_car_fields                         GET      ANY      ANY    /admin/api/car/fields.{_format}
get_car_list                           GET      ANY      ANY    /admin/api/car.{_format}
get_car                                GET      ANY      ANY    /admin/api/cars/{id}.{_format}
post_car                               POST     ANY      ANY    /admin/api/cars.{_format}
put_car                                PUT      ANY      ANY    /admin/api/cars/{id}.{_format}
delete_car                             DELETE   ANY      ANY    /admin/api/cars/{id}.{_format}

Solution

It is not necessary to change the entire tutorial, but it would be nice to remind the reader to work with plurals from the beginning and refer to this Symfony article to avoid this situation.

smirgol commented 6 years ago

Solution 2: Define the singular route(s) in the controller method:

    /**
     * Show a single car-item identified by id
     *
     * @param int $id
     *
     * @Get("car/{id}")
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */

The @Get("car/{id}") does the magic here. Do the same for PUT, DELETE and POST:

Method Route
postCarAction @Post("car")
putCarAction @Put("car/{id}")
deleteCarAction @Delete("car/{id}")

Also include the required namespaces:

use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations\Put;
use FOS\RestBundle\Controller\Annotations\Delete;