chrl/restgeneratorbundle

Symfony 3的REST API生成器

安装: 209

依赖: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 70

类型:symfony-bundle

0.5.0 2017-08-28 16:38 UTC

This package is not auto-updated.

Last update: 2024-09-29 02:54:55 UTC


README

SensioLabsInsight

关于

像CRUD一样的REST生成器

特性

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

安装

在composer.json中要求"voryx/restgeneratorbundle"包,并更新您的依赖项。

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

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

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

配置

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

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 }

生成控制器

生成REST控制器

$ php app/console voryx:generate:rest

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

示例

创建一个名为'Post'的新实体

$ php app/console doctrine:generate:entity --entity=AppBundle:Post --format=annotation --fields="name:string(255) description:string(255)" --no-interaction

更新数据库模式

$ php app/console doctrine:schema:update --force

生成API控制器

$ php app/console voryx:generate:rest --entity="AppBundle:Post"

使用API

如果您选择了默认选项,您将能够像这样开始使用API

创建新的帖子(POST

$ curl -i -H "Content-Type: application/json" -X POST -d '{"name" : "Test Post", "description" : "This is a test post"}' https:///app_dev.php/api/posts

更新(PUT

$ curl -i -H "Content-Type: application/json" -X PUT -d '{"name" : "Test Post 1", "description" : "This is an updated test post"}' https:///app_dev.php/api/posts/1

获取所有帖子(GET

$ curl https:///app_dev.php/api/posts

获取一个帖子(GET

$ curl https:///app_dev.php/api/posts/1

删除(DELETE

$ curl -X DELETE  https:///app_dev.php/api/posts/1

相关实体

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

#Form/PostType()

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