yauhenko / rest-bundle
为 Symfony 6 生成 REST 和 TypeScript
0.9.1
2022-07-13 14:05 UTC
Requires
- php: ^8.1
Requires (Dev)
- doctrine/doctrine-bundle: ^2.2
- doctrine/doctrine-migrations-bundle: ^3.0
- doctrine/orm: ^2.9
- sensio/framework-extra-bundle: ^6.0
- symfony/security-bundle: 6.0.*
- symfony/serializer: ^6.0
- symfony/validator: ^6.0
README
步骤 1: 运行
composer req yauhenko/rest-bundle
步骤 2: 在 config/routes/annotations.yaml
中添加两行
rest_bundle: resource: '@RestBundle/config/routes.yaml'
这将注册 /docs
路由
步骤 3: 在 src/Types.php
中创建 Types 类
<?php namespace App; use Yauhenko\RestBundle\TypesInterface; use Yauhenko\RestBundle\Service\TypeScript; class Types implements TypesInterface { public static function registerTypes(TypeScript $ts): void { // register custom types here (optional) $ts->registerTypeOf('TBadge', ['notifications', 'shop']); // register interfaces $ts->registerInterfacesFromDir(__DIR__ . '/Entity'); $ts->registerInterfacesFromDir(__DIR__ . '/Models'); } public static function codePostProcessor(string $code): string { // change generated TypeScript code here (optional) return $code; } }
您可以通过在 config/services.yaml
中的 yauhenko.rest.types_class
参数更改 Types 类的路径
步骤 4(可选):配置 config/services.yaml
parameters: # Enable TypeScript generation (default: true) yauhenko.rest.ts_enabled: '%env(bool:API_TS_ENABLED)%' # Types class (default) yauhenko.rest.types_class: 'App\Types' # Path to controllers (default) yauhenko.rest.controllers_dir: '%kernel.project_dir%/src/Controller'