【问题标题】:symfony2: How to prevent FOSRestBundle from adding _api to route names and /api to urls?symfony2:如何防止 FOSRestBundle 添加 _api 到路由名称和 /api 到 url?
【发布时间】:2014-07-06 22:57:38
【问题描述】:

我有一个EventApiController 类,如下所示

Class EventApiController
{
    public function getAction($event_id)
    {
      // ...
    }
    public function putAction($event_id)
    {
      // ...
    }
}

使用 Friends of Symfony 捆绑包的路线生成,我希望路线看起来像

CalendarBundle_get_events   [GET]  /api/v1/events/{event_id}.{_format}                                                                                       
CalendarBundle_put_events   [PUT] /api/v1/events/{event_id}.{_format} 

但是,Route 生成器似乎会自动将 post fix /api 添加到所有路由,因此路由看起来像这样。而且文档也没有将其显示为预期的行为。

CalendarBundle_get_events_api   [GET]  /api/v1/events/{event_id}/api.{_format}                                                                                       
CalendarBundle_put_events_api   [PUT] /api/v1/events/{event_id}/api.{_format} 

有谁知道如何从生成的链接中删除 /api 后期修复?我正在使用 FOS/ResutBundle 1.3.1 版

我用于 fos_rest 的 config.yml

fos_rest:
  routing_loader:
    default_format: json
    include_format: true
  view:
     view_response_listener: true

而且我的 Bundle 中的 routing.yml 看起来像这样

event_api:
   type:     rest
   resource: "@CalendarBundle/Controller/EventsApiController.php"
   prefix:   /api/v1
   name_prefix: CalendarBundle_

【问题讨论】:

  • 请将您的fos_rest 配置和相关路由部分添加到问题中。
  • 能否请您使用应用程序中的真实路由更新[GET|PUT] 路由,以便我们可以看到它们与配置的匹配程度,即app/console router:debug | grep CalendarBundle_ 的输出。目前event_apiprefix 路由不在其中...还是您发布了错误的路由配置?
  • 我无法找到任何证据表明将 _api 添加到路由名称或将 /api 添加到 FOSRestBundle 代码中的 url,即 RouteLoader。对我来说,似乎有另一个包负责这种行为。您是否以某种方式额外注释了控制器类/动作?
  • 不,控制器操作只是一个简单的 getAction(),感谢您的帮助,尽管我会进一步调查,并在找到结果时发布结果。
  • 您是否添加了implements ClassResourceInterface 和/或添加了@RouteResource("Event") 注释?见implicit resource name definition

标签: symfony routing fosrestbundle


【解决方案1】:

FOSRestBundleEventApiController 中的Api 检测为路由资源。

您可以像这样覆盖资源名称,以防止_api 路由名称和/api url:

use FOS\RestBundle\Routing\ClassResourceInterface;

/**
 * @RouteResource("Event")
 */
Class EventApiController implements ClassResourceInterface
{

【讨论】:

    猜你喜欢
    • 2015-05-11
    • 2018-06-16
    • 2017-05-22
    • 2019-05-09
    • 2019-05-26
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 2015-01-25
    相关资源
    最近更新 更多