testabit/restgeneratorbundle

用于 Symfony 2 的 REST API 生成器

安装: 157

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 5

分支: 70

类型:symfony-bundle

0.1.0 2014-07-09 15:11 UTC

This package is not auto-updated.

Last update: 2024-09-28 15:34:15 UTC


README

SensioLabsInsight

关于

一个类似于 CRUD 的 REST 生成器

特性

  • 从实体生成 RESTful 动作
  • 简化设置 RESTful 控制器

安装

在您的 composer.json 中添加 "voryx/restgeneratorbundle" 包并更新依赖。

$ php composer.phar require voryx/restgeneratorbundle dev-master

将 VoryxRestGeneratorBundle 添加到您的应用程序内核中,以及其他依赖项。

public function registerBundles()
{
    $bundles = array(
        //...
          new Voryx\RESTGeneratorBundle\VoryxRESTGeneratorBundle(),
          new FOS\RestBundle\FOSRestBundle(),
          new JMS\SerializerBundle\JMSSerializerBundle($this),
          new Nelmio\CorsBundle\NelmioCorsBundle(),
        //...
    );
    //...
}

配置

此包依赖于多个其他 symfony 包,因此它们需要配置,以便生成器能够正常工作。

framework:
    csrf_protection: false #only use for public API

fos_rest:
    routing_loader:
        default_format: json
    param_fetcher_listener: true
    body_listener: true
    #disable_csrf_role: ROLE_USER
    body_converter:
        enabled: true
    view:
        view_response_listener: force

nelmio_cors:
    defaults:
        allow_credentials: false
        allow_origin: []
        allow_headers: []
        allow_methods: []
        expose_headers: []
        max_age: 0
    paths:
        '^/api/':
            allow_origin: ['*']
            allow_headers: ['*']
            allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
            max_age: 3600

sensio_framework_extra:
    request: { converters: true }
    view:    { annotations: false }
    router:  { annotations: true }

生成控制器

目前,生成器使用 doctrine 生成器创建的表单。您必须创建这些表单才能使用生成的控制器。(如果在运行 voryx:generate:rest 命令之前不创建它们,您将收到错误,但之后创建它们仍然可以工作)

$ php app/console doctrine:generate:form AcmeDemoBundle:Post

生成 REST 控制器

$ php app/console voryx:generate:rest

这将引导您通过生成器,为实体生成一个 RESTful 控制器。

您仍然需要为每个生成的实体添加路由:(希望这很快就会被添加到生成器中)

api_posts:
    type:     rest
    resource: "@AcmeDemoBundle/Controller/PostController.php"
    prefix: /api

相关实体

如果您希望表单能够在 POST、PUT 或 PATCH 上将相关实体转换为正确的实体 ID,请使用 voryx_entity 表单类型。

#Form/PostType()

    ->add(
        'user', 'voryx_entity', array(
            'class' => 'Acme\Bundle\Entity\User'
        )
    )