camilord/laravel-postman

将laravel API路由导出为Postman导入JSON文件

1.0.1 2024-05-05 23:11 UTC

This package is auto-updated.

Last update: 2024-09-05 23:48:05 UTC


README

此包允许您将API路由导出到Postman导入JSON文件,由以下工作原创

安装

使用Composer安装包

composer require --dev camilord/laravel-postman

然后在config/app.php中添加服务提供者:

配置

可选地,发布包配置文件

php artisan vendor:publish --provider="Camilo3rd\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 => 跳过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参数和您希望使用的控制器来限制到特定的控制器或控制器列表。