phpsa / laravel-postman
导出 Laravel API 路由到 Postman
Requires
- php: ^8.1
- illuminate/support: ^10|^11
- phpdocumentor/reflection-docblock: ^5.4
Requires (Dev)
- larastan/larastan: ^2.0
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.0
README
此包允许您将 API 路由导出为 Postman 导入 JSON 文件,由 https://github.com/sojeda/laravel-postman 创作的原作。
安装
通过 composer 安装包
composer require --dev phpsa/laravel-postman
然后,在 config/app.php 中添加服务提供者:
配置
可选,发布包配置文件
php artisan vendor:publish --provider="Phpsa\LaravelPostman\ServiceProvider" --tag="config"
注意:发布配置文件是可选的,您可以使用默认的包选项。
选项
- LARAVEL_API_URL => 要在其变量中使用的 API 的 URL
- LARAVEL_POSTMAN_COLLECTION_NAME => 您的集合名称
- LARAVEL_POSTMAN_COLLECTION_DESCRIPTION => 集合的描述
- LARAVEL_POSTMAN_API_PREFIX => 要包含的路线的逗号分隔列表(默认 api,oauth)
- LARAVEL_POSTMAN_API_PREFIX_IGNORE => 要排除的路线的逗号分隔列表(默认 _ignition)
- LARAVEL_POSTMAN_SKIP_HEAD => 跳过头路由 - 默认 true
- LARAVEL_POSTMAN_EXPORT_DIRECTORY => 存储文件的目录,默认为 storage 文件夹
apiURL
这是 Postman 路由的基础 URL
默认值: config('app.url')
collectionName
这是 Postman 集合名称
默认值: 命令将提示输入
collectionDescription
这是 Postman 集合描述
默认值: 命令将提示输入
apiPrefix
这是通过我们识别要导出的路由的前缀
默认值: 'api'
skipHEAD
这避免了为 HEAD 方法创建路由
默认值: true
exportDirectory
将 postman.json 文件导出的目录
用法
配置控制器
在您的实体控制器中添加一个属性,例如这样:
public $postmanModel = 'App\MyEntityModel';
向您的模型类添加一个公共方法,例如这样:(可选)
/** * returns sample body for put/post request * * @param string $method - POST / PUT / PATCH etc. * @param string|null $routeName - the routename from the routes file if set. * * @return array * / public function getPostmanParams(string $method, ?string $routeName): array { return [ 'key1' => 'sampleValue', 'key2' => '' ]; }
此参数数组将用于填充 Postman 中 POST 和 PUT URL 编码表单数据部分。上面的方法只是一个例子,您应该返回您想要在 Postman 中看到的参数数组。
如果未提供上述方法,将使用模型的可填充字段来填充。
记录端点
Laravel-Postman 使用您方法上方的 docblocks 生成文档,例如:
/** * this is the summary - which headlines the description * * this is the description - which goes on a new line under the summary (optional) * * @route-name My Custom Route Name (defaults to folder.model.method if not set) * * @param ... ... */
导出
php artisan postman:export
可选,您可以通过传递 -C
参数和您希望使用的控制器来限制特定的控制器或控制器列表。