【问题标题】:How do routes in FOSRestBundle work?FOSRestBundle 中的路由如何工作?
【发布时间】:2013-02-22 00:10:26
【问题描述】:

有人可以清楚地解释应该如何使用 FOSRest 为 REST 请求配置路由吗?每个教程似乎都有不同的做法。

我的控制器:

<?php
namespace Data\APIBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DatasetController extends Controller{

 protected function postDatasetAction(Request $request){
  //Query here
}

URL 应该是这样的:Symfony/web/app_dev.php/api/dataset。所以我认为路线应该是......

app/config/routes.yml

data_api:
  resource: "@DataAPIBundle/Resources/config/routing.yml"
  prefix: /api
  type: rest

还有……

数据/APIBundle/Resources/config/routing.yml

data_query:
  type: rest
  pattern:  /dataset
  defaults: {_controller: DataAPIBundle:Dataset:datasetAction, _format: json }
  requirements:
     _method: POST

【问题讨论】:

  • 这个问题不清楚,至少对我来说是这样(可能是因为我不知道 FOSRestBundle)但是,你想知道什么?这看起来很清楚:您在yourWebSiteDomain/api/dataset 下有一条路线,您只能通过 post 方法访问,并且请求的格式应该是 json。
  • 似乎用户正在寻求有关如何在 FOSRestBundle 中构建路由的规范教程。正如他所提到的,FOSRestBundle 上的每个教程都以不同的方式处理路由。确实需要一个资源(即项目文档)来清楚地定义可能的变化配置,因为目前尚不清楚所有可能的情况。

标签: php symfony fosrestbundle


【解决方案1】:

请按照下一个网址阅读官方文档: http://symfony.com/doc/master/bundles/FOSRestBundle/index.html

要从这个包开始,我建议遵循 single restful controller 文档: http://symfony.com/doc/master/bundles/FOSRestBundle/5-automatic-route-generation_single-restful-controller.html

您还将找到有关此捆绑包可以提供什么的明确示例 (https://github.com/liip/LiipHelloBundle)。


你发布的 sn-ps 中很少有东西引起了我的注意:

控制器方法的可见性受到保护,而它应该是公开的 (http://symfony.com/doc/current/book/controller.html)

public function postDatasetAction(Request $request) {
     // your code
}

为配置您的路由而创建的“routing.yml”文件应包含上述控制器方法的名称(postDatasetAction 而不是 DatasetAction):

# routing.yml
data_query:
    type: rest
    pattern:  /dataset
    defaults: {_controller: DataAPIBundle:Dataset:postDatasetAction, _format: json }
    requirements:
        _method: POST

请在下面找到一个示例来设置如下路线:

get_items 获取任何 /items.{json}

# config.yml
fos_rest:
    allowed_methods_listener: true

    format_listener:
        default_priorities: ['json', html, '*/*']
        fallback_format: json
        prefer_extension: true

    param_fetcher_listener: true

    routing_loader:
        default_format: json

    view:
        formats:
            json: true
        mime_types:
            json: ['application/json', 'application/x-json']
        force_redirects:
            html: true
        view_response_listener: force

# routing.yml
categories:
    type:     rest
    resource: Acme\DemoBundle\Controller\ItemController

<?php

namespace Acme\DemoBundle\Controller

use FOS\RestBundle\Request\ParamFetcher;
use FOS\RestBundle\Controller\Annotations as Rest;

class ItemController 
{
    /**
     * Get items by constraints
     *
     * @Rest\QueryParam(name="id", array=true, requirements="\d+", default="-1", description="Identifier")
     * @Rest\QueryParam(name="active", requirements="\d?", default="1", description="Active items")
     * @Rest\QueryParam(name="from", requirements="\d{4}-\d{2}-\d{2}", default="0000-00-00", description="From date")
     * @Rest\QueryParam(name="to", requirements="\d{4}-\d{2}-\d{2}", default="0000-00-00", description="End date")
     * @Rest\QueryParam(name="labels", array=true, requirements="\d+", default="-1", description="Labels under which items have been classifed")
     *
     * @Rest\View()
     *
     * @param  ParamFetcher                                          $paramFetcher
     */
    public function getItemsAction(ParamFetcher $paramFetcher) {
        $parameters = $paramFetcher->all();

        // returns array which will be converted to json contents by FOSRestBundle
        return $this->getResource($parameters);
    }
}

附: :您需要添加一个视图以将资源显示为 HTML 页面

【讨论】:

  • 文档和 Liip 包在实际内容/示例方面都相当有限。发布一些此捆绑包的高级用法示例,我将奖励您。例如,自定义路由的使用(Route、Get、Post 等),将请求格式“神奇”地添加到路由,一个可靠的例子,说明如何在从你的行动。
  • 我认为这个解决方案很棒,但我正在寻找其他发生参数注入的地方,类似于 .yml 文件 (%the_date%) 但在 FOS 控制器的动作注释中,该参数将是 self初始化。能做到吗?请检查完整的问题:github.com/FriendsOfSymfony/FOSRestBundle/issues/774
  • @Félix,您可能希望重载 QueryParam 类。
  • @ThierryMarianne,我在哪里可以找到更多事实?
  • @Félix,你可能想学习原文QueryParamgithub.com/FriendsOfSymfony/FOSRestBundle/blob/master/…
【解决方案2】:

您在控制器中缺少 FOSRestbundle 的路由部分:

protected function postDatasetAction(Request $request){

  //Query here

} // "post_dataset"      [POST] /dataset

【讨论】:

  • // "post_dataset" [POST] /dataset 部分
  • 我认为这只是添加到文档中以更好地说明包生成的路由。如果这是错误的,请纠正我。
  • 这只是评论而不是注释。
猜你喜欢
  • 2013-03-14
  • 2014-08-24
  • 1970-01-01
  • 1970-01-01
  • 2013-01-14
  • 2014-04-19
  • 1970-01-01
  • 1970-01-01
  • 2021-06-30
相关资源
最近更新 更多