[Extension] yii2-postman-generator: Postman collections from your controller annotations

Hi everyone,

In Yii2 REST projects, keeping the Postman collection up to date by hand is usually the step that gets skipped. So I wrote a small extension that builds the collection from annotations next to each action:

/**
 * @Postman\Request("List users")
 * @Postman\Folder("Users")
 * @Postman\Path("/api/users")
 * @Postman\Query("page", "1")
 * @Postman\Auth("bearer", "{{token}}")
 */
public function actionIndex() {}

Then php yii postman/generate writes a Postman v2.1 file you can import into Postman, Insomnia, Bruno or Hoppscotch.

Version 1.1.0 just came out with tests on PHP 7.4 to 8.4.

I’d love feedback: what would you need before using it in your project? Reading the routes from UrlManager rules instead of annotations is the next idea on my list.

Great work! One feature I’d love to see is automatic route discovery through Yii2’s UrlManager, along with support for request body parameters, validation rules, and response examples.

Thanks, Aftab! Good list. Here’s where each one stands:

  • Request body: already supported with @Postman\Body({...}) (multi-line JSON, nested objects). What’s missing is building it automatically, which is the next point.
  • Validation rules: I like the idea of @Postman\Model(User::class) (with an optional scenario) that reads rules() and builds the example body: required fields, types from the integer/boolean/email validators, and default values. No more writing the JSON by hand, and it stays in sync with the model. (issue #3)
  • Response examples: @Postman\Response(201, {...}) to save an example on the request, the way Postman shows them under “Examples”. This one is quick to add. (issue #2)
  • Route discovery through UrlManager: the most useful one and the trickiest, because postman/generate runs in the console app while the REST rules live in the web config. The plan: an option that points to your web config, reads the yii\rest\UrlRule rules and works out the method and path for index/view/create/update/delete, so @Postman\Path and @Postman\Method become optional. (issue #4)

Feel free to follow them or comment on the details here: Issues · dannyrios81/yii2-postman-generator · GitHub

Also, v1.2.0 came out yesterday with @Postman\Test(...): it generates test scripts that Postman and Newman run after each request.