Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FOSRestBundle adds 'S' in get URL

// MySomethingController.php

// look no s
public function getSomethingAction($args)
{
    ...
}

// routing.yml

my_something:
    type:     rest
    resource: Blah\Bundle\BlahBundle\Controller\MySomethingController

running:

php app/console router:debug

Output:

[router] Current routes
Name                Method     Pattern
get_something       GET        /somethings/{args}.{_format}

Why is the route 'somethings' ( plural with an 's' ) instead of 'something'?

is this a setting I have somewhere? or is this expected?

like image 820
Phill Pafford Avatar asked Jun 06 '12 20:06

Phill Pafford


2 Answers

after digging in the code:

  • https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Routing/Loader/Reader/RestActionReader.php

Here it is:

private function generateUrlParts(array $resources, array $arguments)
{
    $urlParts = array();
    foreach ($resources as $i => $resource) {
        // if we already added all parent routes paths to URL & we have
        // prefix - add it
        if (!empty($this->routePrefix) && $i === count($this->parents)) {
            $urlParts[] = $this->routePrefix;
        }

        // if we have argument for current resource, then it's object.
        // otherwise - it's collection
        if (isset($arguments[$i])) {
            if (null !== $resource) {
                $urlParts[] =
                    strtolower(Pluralization::pluralize($resource))
                    .'/{'.$arguments[$i]->getName().'}';
            } else {
                $urlParts[] = '{'.$arguments[$i]->getName().'}';
            }
        } elseif (null !== $resource) {
            $urlParts[] = strtolower($resource);
        }
    }

    return $urlParts;
}

I've opened an issue:

  • https://github.com/FriendsOfSymfony/FOSRestBundle/issues/247

in hopes that this would become optional

like image 193
Phill Pafford Avatar answered Oct 19 '22 22:10

Phill Pafford


This is an update answer based on the ticket opened by @phillpafford.

Ismith77 commented on the ticket and I thought explained very well why:

Without the pluralization we wouldn't know the relationship between the methods which is going to be important for example when implementing #52. furthermore its a key idea of REST that we have the GET for a single element in a collection be in a "subdir" of the collection itself.

So if you do "proper" REST then /member/{id}.{_format} would be oddly named but it would be actually wrong if your collection itself wouldn't then also reside under /member{.format}.

The gist of all of this is .. the solution as is isn't so much about convenience than it is about enforcing people actually following REST principles.

PS: However I would like to point out that when you have a word like "data" which is plural on it's own this is a bit annoying...

like image 40
Kirill Fuchs Avatar answered Oct 19 '22 23:10

Kirill Fuchs