gpt4 book ai didi

php - FOSRestBundle 中的路由是如何工作的?

转载 作者:IT王子 更新时间:2023-10-28 23:54:41 24 4
gpt4 key购买 nike

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

我的 Controller :

<?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

还有....

Data/APIBundle/Resources/config/routing.yml

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

最佳答案

请按照以下网址阅读官方文档: 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

您还将找到有关此 bundle 可以提供的内容的清晰示例 ( https://github.com/liip/LiipHelloBundle )。


您发布的片段中有几件事引起了我的注意:

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

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

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

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

请在下面找到设置路由的示例:

get_items GET ANY ANY/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);
}
}

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

关于php - FOSRestBundle 中的路由是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15260278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com