pgs-soft/restfony-bundle

此包已被放弃,不再维护。未建议替代包。

Symfony3 包,用于实现 RESTful CRUD

安装数: 3,202

依赖: 0

建议者: 0

安全: 0

星标: 6

关注者: 5

分支: 0

开放问题: 4

类型:symfony-bundle

1.0.0 2016-08-10 09:35 UTC

This package is auto-updated.

Last update: 2020-02-17 08:20:59 UTC


README

Latest Stable Version Minimum PHP Version License Build Status Code Coverage Scrutinizer Code Quality

用于辅助创建 Doctrine 实体类的 Bundle,旨在加速创建 RESTful API。

安装

使用 Composer 安装包

    composer require pgs-soft/restfony-bundle

在内核中启用包(以及依赖包)

    <?php
    // app/AppKernel.php
    
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Pgs\RestfonyBundle\PgsRestfonyBundle(),
            new FOS\RestBundle\FOSRestBundle(),
            new JMS\SerializerBundle\JMSSerializerBundle(),
            new Nelmio\ApiDocBundle\NelmioApiDocBundle(),
            new Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle(),
            new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
            new Lexik\Bundle\FormFilterBundle\LexikFormFilterBundle(),
            // ...
        );
    }

为 Restfony 和 FOSRestBundle 准备配置

    # app/config/rest.yml
    
    jms_serializer:
        handlers:
            datetime:
                default_format: 'Y-m-d\TH:i:sP'
        property_naming:
            separator: null
            lower_case: false
    fos_rest:
        param_fetcher_listener: true
        body_listener: true
        format_listener: true
        routing_loader:
            default_format: json
            include_format: false
        serializer:
            serialize_null: true
    pgs_restfony:
        modules:

并将其包含在主配置中

    # app/config/config.yml
    
    imports:
    # ...
        - { resource: rest.yml }
    
    # ...

添加路由定义,例如:

    # app/config/routing.yml 
    
    # ...
    
    appbundle_rest:
        resource: "@AppBundle/Resources/config/rest_routing.yml"
        prefix:   /api/

使用方法

对实体运行命令

    bin/console pgs:generate:crud MyEntity

提供实体快捷名称(例如 AppBundle:MyEntity),决定是否添加“写”操作和生成路由。

将生成新文件

  • Controller/MyEntityController.php - 包含 RESTful 动作和 ApiDoc 的控制器
  • Form/Filter/MyEntityFilterType.php - 表单过滤器类
  • Form/Type/MyEntityType.php - 表单类型类
  • Manager/MyEntityManager.php - 用于管理存储库的空类
  • Manager/MyEntityManagerInterface.php - 上面管理器的接口
  • Repository/MyEntityRepository.php - 空的存储库类
  • Resources/config/serializer/Entity.MyEntity.yml - JMS Serializer 的配置
  • Tests/MyEntityControllerTest.php - 为生成操作设置测试

并将更新配置

  • rest.yml - 添加 MyEntity 模块
  • rest_routing.yml - 添加 MyEntity 条目

API 文档

如果需要,可以添加 NelmioApiDocBundle API 文档路由,例如:

    # app/config/routing_dev.yml
    
    # ...
    
    NelmioApiDocBundle:
        resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
        prefix:   /api/doc

作者

贡献者