lambelcebur / doctrine-orm-fast-api
快速使用Doctrine ORM连接创建自动API CRUD
dev-master
2020-01-27 14:51 UTC
Requires
- php: ^7.1
- doctrine/doctrine-module: 2.*
- doctrine/doctrine-orm-module: 2.*
- zendframework/zend-filter: 2.*
Suggests
- lambelcebur/doctrine-orm-resources: DoctrineORM Resources to Laminas
- lambelcebur/materialize-css-helper: MaterializeCss Helper integration to Laminas
- lambelcebur/mongodb-logger: MongoDb Logger integration to Laminas
- lambelcebur/mvc-basic-tools: Laminas tools for controllers,views,helpers, plugins and more ...
- lambelcebur/oauth2-social-client: Laminas extends module from thephpleague/oauth2-client
- 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.
This package is not auto-updated.
Last update: 2024-10-02 11:59:09 UTC
README
快速使用Doctrine ORM连接创建自动API CRUD
查看
安装
此模块的安装使用composer。有关composer文档,请参阅 getcomposer.org。
composer require lambelcebur/doctrine-orm-fast-api
然后添加 LamBelcebur\DoctrineORMFastApi
到您的 config/application.config.php
如何使用?
LamBelcebur\DoctrineORMFastApi\Controller\IndexController
继承 AbstractRestfulController
并为所有方法提供自动代码。
- 所有视图都返回 JsonModel
配置自定义路由,默认为 /bapi
[ return [ ...other configs 'LamBelcebur\DoctrineORMFastApi' => [ 'route' => [ 'bapi' => [ 'type' => \Laminas\Router\Http\Literal::class, 'options' => [ 'route' => '/my-custom-url', ], ], ], ], ]; ]
获取可用的API
- 访问url
/bapi
获取列表
- 访问url
/bapi/entity-name
获取列表
- 从路由参数中获取EntityManager或Paginator对象
<?php /** @var JsonModel $exchangeMessagesJsonView */ use Laminas\View\Model\JsonModel;use LamBelcebur\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 'LamBelcebur\DoctrineORMFastApi' => [ 'naming_strategy' => DashNamingStrategy::class, ], ]; ]
添加新策略
- 您可以通过fieldName、fieldType或relation添加其他策略。
示例
- 首先通过类型或名称在配置文件中添加您的策略
[ 'LamBelcebur\DoctrineORMFastApi' => [ 'hydrator-value-strategy-by-type' => [ 'datetime' => \Laminas\Hydrator\Strategy\DateTimeFormatterStrategy::class, ], 'hydrator-value-strategy-by-name' => [ 'languages' => \Laminas\Hydrator\Strategy\ExplodeStrategy::class, ], ...other configs ] ]
- 删除
reflection-file-path
文件以重新生成配置。默认为getcwd() . '/config/autoload/lambelcebur-doctrine-orm-fast-api.global.php'
自定义提取函数
- 在您的实体中使用
\LamBelcebur\DoctrineORMFastApi\Resource\DoctrineORMFastApiInterface
并实现您的自定义方法
namespace LamBelcebur\DoctrineORMFastApi\Resource; use Doctrine\ORM\EntityManager; use Laminas\Hydrator\HydratorInterface; interface DoctrineORMFastApiInterface { public function toDoctrineORMFastApi(EntityManager $em,HydratorInterface $hydrator): array; }
默认配置
return [ __NAMESPACE__ => [ 'reflection-file-path' => getcwd() . '/config/autoload/lambelcebur-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, ], ], ];