dskripchenko / laravel-api
Laravel API 模块。
2.5.2
2024-07-09 07:55 UTC
Requires
- ext-json: *
- dskripchenko/php-array-helper: ^1.0
- laravel/framework: 6.*|7.*|8.*|9.*|10.*|11.*
- phpdocumentor/reflection-docblock: *
Requires (Dev)
- roave/security-advisories: dev-master
README
运行
php composer.phar require dskripchenko/laravel-api "^2.0.0"
或添加
"dskripchenko/laravel-api": "^2.0.0"
到你的 composer.json
文件的 require
部分。
使用方法
参见 Dskripchenko\LaravelApiExample\
- 创建
ApiModule
类,继承自Dskripchenko\LaravelApi\Components\BaseModule
- 创建
Api
类,继承自Dskripchenko\LaravelApi\Components\BaseApi
- 定义
getMethods
方法 - 在
ApiModule
中覆盖getApiVersionList
,返回数组['version' => Api::class]
- 创建
ApiServiceProvider
类,继承自Dskripchenko\LaravelApi\Providers\ApiServiceProvider
- 在新的
ApiServiceProvider
中覆盖getApiModule
- 注册
ApiServiceProvider
API 方法模板
use Dskripchenko\LaravelApi\Components\BaseApi; class Api extends BaseApi { public static function getMethods(): array { return [ 'controllers' => [ '{controllersKey1}' => [ 'controller' => '{Controller::class}', 'actions' => [ '{actionKey1}', // api/version/{controllersKey1}/{actionKey1} '{actionKey2}' => '{real method name}', //api/version/{controllersKey1}/{actionKey2} '{actionKey3}' => false, //disable //api/version/{controllersKey1}/{actionKey3} '{actionKey4}' => [ 'exclude-all-middleware' => true, //optional, exclude all global and controller middleware 'middleware' => [ "{Middleware::class}" ], //optional, define specific middleware 'exclude-middleware' => [], //optional, exclude specific middleware ], ], 'exclude-all-middleware' => true, //optional, ... 'middleware' => [], //optional, ... 'exclude-middleware' => [], //optional, ... ] ], 'middleware' => [] //optional, ... ]; } }
自动文档注释到 Swagger
/** * Method title * Method description * * @input type $requiredVariable1 name1 * @input type ?$optionalVariable2 name2 * @input [getSwaggerMetaInputs] * * @output type $variable1 name1 * @output type $variable2 name2 */
可用类型 string|number|integer|boolean|file
组件
- BaseApi
- getMethods
- getSwaggerTemplates
- beforeCallAction
- afterCallAction
- getDefaultEmptyResponse
- getMiddleware
- registerApiComponent
- BaseModule
- makeApi
- getApiMiddleware
- getApiPrefix
- getAvailableApiMethods
- getApiUriPattern
- getApiVersionList
- 元数据
- 操作
- 创建
- 读取
- 更新
- 删除
- CRUD
- 列
- 字符串
- 布尔型
- 数字
- 整数
- 隐藏
- 选择
- 文件
- getSwaggerInputs
- toArray
- 操作
控制台
命令
- ApiInstall
- getEnvConfig
- fillEnvironment
- reloadEnvironment
- onBeginSetup
- onEndSetup
- BaseCommand
- askValid
- validateInput
控制器
- ApiController
- 成功
- 错误
- 验证错误
- ApiDocumentationController
- 索引
- CrudController
- 元数据
- 搜索
- 创建
- 读取
- 更新
- 删除
- getSwaggerMetaInputs
异常
- ApiErrorHandler
- addErrorHandler
- handle
- ApiException
- getErrorKey
- 处理器
- render
外观
- ApiRequest
- getApiVersion
- getApiMethod
- getApiControllerKey
- getApiActionKey
- ApiModule
- getApiPrefix
- getAvailableApiMethods
- getApiUriPattern
- getApiMiddleware
- getApiVersionList
- makeApi
- ApiErrorHandler
- addErrorHandler
- handle
中间件
- ApiMiddleware
- run
提供者
- ApiServiceProvider
- getApiModule
- getApiErrorHandler
- getApiRequest
- makeApiRoutes
- BaseServiceProvider
- mergeConfigFrom
请求
- BaseApiRequest
- validateApiUriPattern
- prepareApi
- CrudSearchRequest
资源
- BaseJsonResource
- BaseJsonResourceCollection
服务
- ApiResponseHelper
- say
- sayError
- CrudService
- 元数据
- query
- resource
- collection
- 搜索
- 创建
- 读取
- 更新
- 删除
特质
- SwaggerApiTrait
- getSwaggerApiConfig
接口
- CrudServiceInterface
- 元数据
- query
- resource
- collection
- 搜索
- 创建
- 读取
- 更新
- 删除
示例
- 注册
Dskripchenko\LaravelApiExample\ExampleApiServiceProvider
- 运行
php artisan vendor:publish
- 运行
php artisan storage:link
- 打开网页
/api/doc