标签: fosrestbundle

使用FOSRestBundle和Symfony混合路由和查询参数

使用Symfony2和FOSRestBundle我试图实现API方法,这些方法在路由中定义了一些固定参数以及查询字符串中可能存在的一些可选参数.

例如:

 http://somesite.com/api/method/a/b
 http://somesite.com/api/method/c/d?x=1&y=2
Run Code Online (Sandbox Code Playgroud)

根据FOSRestBundle的文档,ParamFetcher是使用@QueryParam注释执行此操作的正确方法.但是,我已经有一个控制器定义如下:

 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use FOS\RestBundle\Controller\Annotations\Get;
 use FOS\RestBundle\Controller\Annotations\View;

 class MyController extends Controller
 {

   /**
    * @Get("/method/{a}/{b}")
    * @View()
    */
   public function getMethodAction($a, $b)
   {
     // do stuff

     return array('foo' => 'bar');
   }

 }
Run Code Online (Sandbox Code Playgroud)

现在看来我需要能够访问ParamFetcher的实例,但我不知道如何(谷歌搜索没有多大帮助).我从文档中知道我可以简单地更改方法签名以包含ParamFetcher,但是,当我这样做时,它将参数移动到查询字符串中,这是我不能拥有的.

有没有办法混合两者,或者我应该放弃ParamFetcher并直接使用Symfomy的内置Request对象检查请求?

symfony symfony-2.1 fosrestbundle

8
推荐指数
2
解决办法
1万
查看次数

Symfony2 FOSRestBundle PUT Action FORM返回空结果

我正在使用Symfony 2.2和最新版本的FOSRestBundle.所以我设法让大多数动作都有效,但我似乎遇到了FormBuilder的问题,我正在通过PUT调用请求.

我检查了请求对象,它来自我的Backbone.je模型​​,因为它应该(.save())但是在绑定到表单后,实体返回只有id导致flush()抛出错误,因为必需的字段没有填补.

控制器中的操作:

header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS ');
header('Allow GET, POST, PUT, DELETE, OPTIONS ');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type, *');

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Routing\ClassResourceInterface;
use FOS\Rest\Util\Codes;
use Symfony\Component\HttpFoundation\Request;
use Greenthumbed\ApiBundle\Entity\Container;
use Greenthumbed\ApiBundle\Form\ContainerType;

class ContainerController extends FOSRestController implements ClassResourceInterface
{
/**
 * Put action
 * @var Request $request
 * @var integer $id Id of the entity
 * @return View|array
 */
public function putAction(Request $request, $id)
{
    $entity = $this->getEntity($id);
    $form = $this->createForm(new …
Run Code Online (Sandbox Code Playgroud)

php symfony-forms symfony fosrestbundle

8
推荐指数
3
解决办法
9746
查看次数

FosRestBundle post/put [create new/update entity]无法正确读取请求

长话短说:使用FOSRestBundle我试图通过POST调用创建一些实体,或通过PUT修改现有实体.

这里的代码:

/**
 * Put action
 * @var Request $request
 * @var integer $id Id of the entity
 * @return View|array
 */
public function putCountriesAction(Request $request, $id)
{
    $entity = $this->getEntity($id);
    $form = $this->createForm(new CountriesType(), $entity, array('method' => 'PUT'));
    $form->bind($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();
        return $this->view(null, Codes::HTTP_NO_CONTENT);
    }

    return array(
        'form' => $form,
    );
} //[PUT] /countries/{id}
Run Code Online (Sandbox Code Playgroud)

如果我通过PUT调用/ countries/{id}并传递像{"description":"Japan"}这样的json,它会修改我的国家/地区id = 1,并输入一个空的描述.

相反,如果我尝试使用此方法创建一个新实体:

/**
 * Create new Countries (in batch)
 * @param  Request $request json request
 * …
Run Code Online (Sandbox Code Playgroud)

post put request symfony fosrestbundle

8
推荐指数
1
解决办法
1万
查看次数

如何在Symfony2中跟踪API使用情况?

我正在使用FOSRestBundle在Symfony2中构建RESTful服务.我可以使用Google Analytics跟踪网络客户端中的网页使用情况.但是,这显然不适用于非HTML客户端的请求.

在我开始安装Redis,编写服务,事件调度程序等之前,这个问题已经解决了吗?是否存在对性能没有严重影响的解决方案?

根据我正在更换的项目统计数据,我预计每小时大约有1,000次点击,其中90%的流量来自浏览器.我不会控制非HTML客户端,因此添加跟踪不是一个选项.

我需要数据的原因与任何人都需要分析数据相同 - 制作漂亮的图表,并提供关于在何处集中开发资源的定量证据.

rest analytics symfony fosrestbundle

7
推荐指数
1
解决办法
808
查看次数

FOSRestBundle - 仅允许json + xml,如果没有其他请求则默认为json

有人可以给我看一个FOSRestBundle的参考配置,它只允许json + xml和willd默认为json,如果没有设置"Accepted"或"Content-Type".

我的目标是删除模板html支持和所有其他不必要的格式(在我的情况下).

提前致谢!

php symfony fosrestbundle

7
推荐指数
1
解决办法
3829
查看次数

通过 PUT 方法上传 Symfony REST 文件

我有一个 REST 服务,我想通过 PUT 更新文件。当我使用 POST 时,我使用以下内容来获取上传的文件:

/**
 * @var Request $request
 */
$request->files->get('file');
Run Code Online (Sandbox Code Playgroud)

如何在Symfony Framework中将上传的文件作为PUT发送?

php rest symfony fosrestbundle

7
推荐指数
1
解决办法
4563
查看次数

在Symfony中的REST api中创建链接的实体

创建与其他实体相关联的实体的正确方法是什么?我找不到任何有关如何使用FOSRestBundle包在Symfony中完成或如何完成的来源.

背景:

让我们有实体Car与Wheel实体.

如何在一个请求中创建带轮子的汽车?
url: foo.com/cars POST
数据: {car:{name="porsche", wheels:[{name:"fr"}, {name:"fl"}] }}
这是怎么做的正确方法?我读了一些关于LINK和UNLINK http方法的内容,但是需要发送多个请求才能创建所有轮子,然后将它们链接在一起.

如何制造属于多辆车的车轮?
url: foo.com/wheels POST
数据: {wheel:{name="super wheel", cars:[{id:1}, {id:2}] }}
在这种情况下,可能使用LINK标头是合适的.但是看完之后这个文章,它会要求对所有岗位解析LINK头,并提出为好,使之昂贵和笨重.

编辑

我写信给提到的文章的作者.这是他的答案:

我想你可以在一个请求中发送包含你的轮子和汽车的数据.这里最重要的是要务实.构建REST API没有"明确"的规则.

当资源已经存在时,LINK/UNLINK非常有用,并且您想要"链接"它们,换句话说,您希望在它们之间添加"关系",例如用户的友谊.

关于你的第二个问题,如果汽车存在,你可以首先创建你的车轮,然后将它"链接"到你的车上.但这意味着有两个请求执行此操作(您可以在一个请求中添加多个链接头),或者更好的是您可以发送一个带有链接头的POST请求:一个请求将它们全部统治:p

另一个选择是将汽车的ID引用到您的车轮数据中.我觉得这样做也没关系.

rest entity hyperlink symfony fosrestbundle

7
推荐指数
1
解决办法
972
查看次数

当我在FOS Rest Bundle中拥有多级子资源时,每个父控制器必须具有`get {SINGULAR} Action($ id)`方法

我有三个名为控制器BlogController,PostController,CommentControllerCommentController是子资源PostControllerPostController子资源BlogController.

/**
 * @Rest\RouteResource("blog", pluralize=false)
 */
class BlogController extends FOSRestController
{
    public function getAction($blogUri)
    {
    ...
    }
}

/**
 * @Rest\RouteResource("post", pluralize=false)
 */
class PostController extends FOSRestController
{
    public function getAction($postId)
    {
    ...
    }
}

/**
 * @Rest\RouteResource("comment", pluralize=false)
 */
class CommentController extends FOSRestController
{
    public function getAction($commentId)
    {
    ...
    }
}
Run Code Online (Sandbox Code Playgroud)

使用routing.yml

mgh_blog:
    resource: MGH\BlogBundle\Controller\BlogController
    type:     rest

mgh_blog_post:
    resource: MGH\BlogBundle\Controller\PostController
    type:     rest
    parent:   mgh_blog

mgh_blog_post_comment: …
Run Code Online (Sandbox Code Playgroud)

symfony fosrestbundle symfony-2.7

7
推荐指数
1
解决办法
996
查看次数

JMS Serializer:如何将camel case用于属性

我正在使用FOS Rest bundle和JMS Serializer来创建REST Api.问题是我想保留JSON响应中的属性名称,而不是使用_.

例如,我有一个名为employeeIdentifier的属性,默认情况下会转换为employee_identifier.

我看到配置中有一个选项可以禁用小写并删除_,但随后它变为EmployeeIdentifier.

有没有什么方法JMS Serializer保留属性的原始名称?提前致谢

serialization symfony fosrestbundle jmsserializerbundle

7
推荐指数
2
解决办法
7008
查看次数

JMS Serializer覆盖FOSRestBundle中的组

我读了这篇关于覆盖子属性组的文章:

use JMS\Serializer\SerializationContext;

$context = SerializationContext::create()->setGroups(array(
    'Default', // Serialize John's name
    'manager_group', // Serialize John's manager
    'friends_group', // Serialize John's friends

    'manager' => array( // Override the groups for the manager of John
        'Default', // Serialize John manager's name
        'friends_group', // Serialize John manager's friends. If you do not override the groups for the friends, it will default to Default.
    ),

    'friends' => array( // Override the groups for the friends of John
        'manager_group' // Serialize …
Run Code Online (Sandbox Code Playgroud)

php symfony fosrestbundle jmsserializerbundle

7
推荐指数
1
解决办法
1671
查看次数