FOSRestBundle 在 get URL 中添加了“S”


FOSRestBundle adds 'S' in get URL

//MySomethingController.php

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

路由.yml

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

运行:

php app/console router:debug

输出:

[router] Current routes
Name                Method     Pattern
get_something       GET        /somethings/{args}.{_format}
为什么路线是"某物"

(带有"s"的复数形式)而不是"某物"?

这是我在某处拥有的设置吗? 还是意料之中的?

在挖掘代码后:

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

在这里:

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;
}

我打开了一个问题:

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

希望这将成为可选的

这是

基于@phillpafford打开的票证的更新答案。

Ismith77评论了这张票,我想很好地解释了原因:

没有复数,我们就不会知道两者之间的关系 重要的方法,例如,当 实施#52。此外,这是REST的一个关键思想,我们拥有 集合中单个元素的 GET 位于 集合本身。

因此,如果您执行"适当的"REST,则为/member/{id}。{_format}会很奇怪 命名,但如果您的收藏本身实际上是错误的 然后不会也驻留在/member{.format} 下。

所有这一切的要点是..解决方案不是那么多 方便性比强制人们实际遵循 REST 更方便 原则。

PS:但是我想指出的是,当您有一个像"数据"这样的词本身就是复数时,这有点烦人......