zf3belcebur / doctrine-orm-fast-api
此包已被弃用且不再维护。未建议替换包。
使用您的 Doctrine ORM 连接快速创建自动 API CRUD
v1.0.2
2019-10-04 07:24 UTC
Requires
- php: ^7.1
- doctrine/doctrine-module: 2.*
- doctrine/doctrine-orm-module: 2.*
- zendframework/zend-filter: 2.*
Suggests
- nesbot/carbon: A API extension for DateTime that supports 281 different languages.
- zendframework/zend-form: Necessary to use FormManager - Validate and display simple and complex forms, casting forms to business objects and vice versa.
- zendframework/zend-hydrator: Serialize objects to arrays, and vice versa, strategies.
- zf3belcebur/doctrine-orm-resources: DoctrineORM Resources to ZF3
- zf3belcebur/materialize-css-helper: MaterializeCss Helper integration to ZF3
- zf3belcebur/mongodb-logger: MongoDb Logger integration to ZF3
- zf3belcebur/mvc-basic-tools: ZF3 tools for controllers,views,helpers, plugins and more ...
- zf3belcebur/oauth2-social-client: ZF3 extends module from thephpleague/oauth2-client
This package is auto-updated.
Last update: 2022-08-04 13:49:08 UTC
README
使用您的 Doctrine ORM 连接快速创建自动 API CRUD
查看
安装
此模块的安装使用 composer。有关 composer 文档,请参阅 getcomposer.org。
composer require zf3belcebur/doctrine-orm-fast-api
然后,将 ZF3Belcebur\DoctrineORMFastApi
添加到您的 config/application.config.php
如何使用?
ZF3Belcebur\DoctrineORMFastApi\Controller\IndexController
继承自 AbstractRestfulController
并为所有方法提供自动代码。
- 所有视图都返回 JsonModel
配置自定义路由,默认为 /bapi
[ return [ ...other configs 'ZF3Belcebur\DoctrineORMFastApi' => [ 'route' => [ 'bapi' => [ 'type' => \Zend\Router\Http\Literal::class, 'options' => [ 'route' => '/my-custom-url', ], ], ], ], ]; ]
获取可用的 API
- 访问 URL
/bapi
获取列表
- 访问 URL
/bapi/entity-name
获取列表
- 从路由参数获取 EntityManager 或 Paginator 对象
<?php /** @var JsonModel $exchangeMessagesJsonView */ use Zend\View\Model\JsonModel;use ZF3Belcebur\DoctrineORMFastApi\Controller\IndexController;$exchangeMessagesJsonView = $this->forward()->dispatch(IndexController::class, [ 'entity' => YourEntity::class, 'options' => [ 'paginator' => true, //set paginator object in return 'entityManager' => true, //set entityManager in return 'hydrate' => false, //set items like objects in return ], ]);
带有过滤器的列表获取
- 访问 URL
/bapi/entity-name?propertyOrRelationName=propertyOrRelationValue
- 要使用布尔值进行过滤,需要将其转换为整数
- /bapi/product?enable=1
- /bapi/product?enable=1&family=7
获取
- 向 URL
/bapi/entity-name/identifierValue
发送 GET 请求- /bapi/product/25687
更新
- 向 URL
/bapi/entity-name/identifierValue
发送 PUT 请求- PUT -> /bapi/product/25687
创建
- 向 URL
/bapi/entity-name/identifierValue
发送 POST 请求- POST -> /bapi/product/25687
删除
- 向 URL
/bapi/entity-name/identifierValue
发送 DELETE 请求- DELETE -> /bapi/product/25687
表单验证
- 如果您需要使用 ZendForm 验证数据,只需将表单名称添加到 URL 以从 FormElementManager 获取即可
- /bapi/product/25687/my-form-name
命名策略
[ return [ ...other configs 'ZF3Belcebur\DoctrineORMFastApi' => [ 'naming_strategy' => DashNamingStrategy::class, ], ]; ]
添加新策略
- 您可以通过 fieldName、fieldType 或 relation 添加其他策略。
示例
- 首先,通过类型或名称在配置文件中添加您的策略
[ 'ZF3Belcebur\DoctrineORMFastApi' => [ 'hydrator-value-strategy-by-type' => [ 'datetime' => \Zend\Hydrator\Strategy\DateTimeFormatterStrategy::class, ], 'hydrator-value-strategy-by-name' => [ 'languages' => \Zend\Hydrator\Strategy\ExplodeStrategy::class, ], ...other configs ] ]
- 删除
reflection-file-path
文件以重新生成配置。默认情况下getcwd() . '/config/autoload/zf3belcebur-doctrine-orm-fast-api.global.php'
自定义提取函数
- 在您的实体中使用
\ZF3Belcebur\DoctrineORMFastApi\Resource\DoctrineORMFastApiInterface
并实现您的自定义方法
namespace ZF3Belcebur\DoctrineORMFastApi\Resource; use Doctrine\ORM\EntityManager; use Zend\Hydrator\HydratorInterface; interface DoctrineORMFastApiInterface { public function toDoctrineORMFastApi(EntityManager $em,HydratorInterface $hydrator): array; }
默认配置
return [ __NAMESPACE__ => [ 'reflection-file-path' => getcwd() . '/config/autoload/zf3belcebur-doctrine-orm-fast-api.global.php', 'naming_strategy' => DashNamingStrategy::class, 'strategies' => [ EntityStrategy::class => EntityStrategy::class, CollectionStrategy::class => CollectionStrategy::class, ], 'reflection-table' => [], 'hydrator-relation-strategy' => [ ClassMetadata::MANY_TO_ONE => EntityStrategy::class, ClassMetadata::ONE_TO_ONE => EntityStrategy::class, ClassMetadata::ONE_TO_MANY => CollectionStrategy::class, ClassMetadata::MANY_TO_MANY => CollectionStrategy::class, ], 'hydrator-value-strategy-by-type' => [ //'datetime' => DateTimeFormatterStrategy::class, ], 'hydrator-value-strategy-by-name' => [ //'languages' => ExplodeStrategy::class, //'defaultLocale' => ExplodeStrategy::class, ], 'hydrator-value-strategy-by-class' => [], 'route' => [ __NAMESPACE__ => [ 'type' => Literal::class, 'options' => [ 'route' => '/bapi', 'defaults' => [ 'controller' => IndexController::class, ], ], 'may_terminate' => true, 'child_routes' => [ 'entity' => [ 'type' => Segment::class, 'options' => [ 'route' => '/:entity[/:id][/:form]', 'constraints' => [ 'entity' => '[a-zA-Z0-9-]+', 'form' => '[a-zA-Z0-9-]+', 'id' => '[a-zA-Z0-9-]+', ], 'defaults' => [ 'form' => null, ], ], ], ], ], ], ], 'controllers' => [ 'factories' => [ IndexController::class => IndexControllerFactory::class, ], ], ];