Skip to content
This repository was archived by the owner on Jan 14, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# laravel-postman
This package allows you to export your API routes to a postman import json file
This package allows you to export your API routes to a postman import json file, original work by https://github.com/sojeda/laravel-postman

## Installation

Install the package via composer

`composer require --dev jimenezmaximiliano/laravel-postman`
`composer require --dev phpsa/laravel-postman`

Then add the service provider in config/app.php:

### PHP >= 5.5

`JimenezMaximiliano\LaravelPostman\LaravelPostmanServiceProvider::class`
`Phpsa\LaravelPostman\ServiceProvider::class`

### PHP < 5.5

`JimenezMaximiliano\LaravelPostman\LaravelPostmanServiceProvider`
`Phpsa\LaravelPostman\ServiceProvider`

## Configuration

First, publish the package configuration file:

`php artisan vendor:publish`
`php artisan vendor:publish --provider="Phpsa\LaravelPostman\ServiceProvider" --tag="config"`

Note: publishing the configuration file is optional, you can use de default package options.

Expand Down Expand Up @@ -78,4 +78,4 @@ params that you want to see in postman.

### Export

`php artisan laravelPostman:export`
`php artisan laravelPostman:export`
32 changes: 19 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
{
"name": "jimenezmaximiliano/laravel-postman",
"name": "phpsa/laravel-postman",
"type": "library",
"description": "Export laravel API routes to postman",
"keywords": [
"jimenezmaximiliano",
"laravel",
"postman",
"github",
"export",
"api"
],
"homepage": "https://github.com/jimenezmaximiliano/laravel-postman",
"homepage": "https://github.com/phpsa/laravel-postman",
"license": "MIT",
"authors": [
{
"name": "Craig Smith",
"email": "vxdhost@gmail.com",
"role" : "developer"

},
{
"name": "Maximiliano Jimenez",
"email": "jimenez@maximiliano.com.ar",
Expand All @@ -21,27 +26,28 @@
}
],
"require": {
"illuminate/support": "~5",
"illuminate/console": "~5",
"php" : ">=5.4"
"illuminate/support": "^6.0",
"php" : ">=7.1"
},
"require-dev": {
"phpunit/phpunit" : "~4.0||~5.0"
"phpunit/phpunit" : "^7.0||^8.0"
},
"autoload": {
"psr-4": {
"JimenezMaximiliano\\LaravelPostman\\": "src"
"Phpsa\\LaravelPostman\\": "src"
}
},
"scripts": {
"test": "phpunit",
"format": "phpcbf --standard=psr2 src/"
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
},
"extra": {
"laravel": {
"providers": [
"Phpsa\\LaravelPostman\\ServiceProvider"
]
}
},
"config": {
"sort-packages": true
}
Expand Down
114 changes: 60 additions & 54 deletions src/Helper.php
Original file line number Diff line number Diff line change
@@ -1,81 +1,81 @@
<?php

namespace JimenezMaximiliano\LaravelPostman;
namespace Phpsa\LaravelPostman;

class Helper
{
const CONTROLLER_STRING_INDEX = 0;
const POSTMAN_SCHEMA = 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json';

/**
* Class constructor
*/
public function __construct()
{
}

/**
* Adds a trailing slash to the given path if it isn't already there
*
*
* @param string $path
* @return string
*/
public function addTrailingSlash($path)
{
return $path . (substr($path, -1) === '/' ?: '/');
}

/**
* Replaces laravel route parameters format with Postman parameters format
*
*
* @param string $path
* @return string
*/
public function replaceGetParameters($path)
{
return str_replace(['{', '}'], [':', ''], $path);
}

/**
* Returns the API base URL
*
*
* @return string
*/
public function getBaseURL()
{
$configURL = config('postman.apiURL');

if (!empty($configURL)) {

return $this->addTrailingSlash($configURL);
}

return $this->addTrailingSlash(config('app.url'));
}

/**
* Returns the API prefix string
*
*
* @return string
*/
public function getApiPrefix()
{
$apiPrefix = config('postman.apiPrefix');

return !empty($apiPrefix) ? $apiPrefix : 'api';
}

/**
* Returns a postman collection structure array
*
*
* @param string $collectionName
* @param string $collectionDescription
* @return array
*/
public function getCollectionStructure(
$collectionName,
$collectionDescription)
{
$collectionName,
$collectionDescription
) {
return [
'variables' => [],
'info' => [
Expand All @@ -87,100 +87,106 @@ public function getCollectionStructure(
'item' => [],
];
}

/**
* Obtains a postman folder name from the given laravel route
*
*
* @param Illuminate\Routing\Route $route
* @return string
*/
public function getRouteFolder($route)
{
$actionStringParts = explode('@', $route->getActionName());

if (count($actionStringParts) === 1) {
return 'Others';

return 'Others';
}

$fullController = $actionStringParts[self::CONTROLLER_STRING_INDEX];
$controllerClass = explode('\\', $fullController);

return str_replace('Controller', '', last($controllerClass));
}

/**
* Returns the path where the exported file will be located
*
*
* @return string
*/
public function getExportDirectory()
{
$exportDirectory = config('postman.exportDirectory');

if (empty($exportDirectory)) {

return $exportDirectory;
}

return $this->addTrailingSlash($exportDirectory);
}

/**
* Finds out if a postman model can be get from the route
*
*
* @param Illuminate\Routing\Route $route
* @return boolean
*/
public function canGetPostmanModel($route)
{
if (method_exists($route, 'getController')
&& is_object($route->getController())
&& property_exists($route->getController(), 'postmanModel')) {

if (
method_exists($route, 'getController')
&& is_object($route->getController())
&& property_exists($route->getController(), 'postmanModel')
) {

return true;
}

if (method_exists($route, 'getAction')
&& is_array($route->getAction())
&& in_array('controller', array_keys($route->getAction()))) {


if (
method_exists($route, 'getAction')
&& is_array($route->getAction())
&& in_array('controller', array_keys($route->getAction()))
) {

return true;
}

return false;
}

/**
* Returns a route's postman model
*
*
* @param Illuminate\Routing\Route $route
* @return object|null
*/
public function getPostmanModel($route)
{
if (!$this->canGetPostmanModel($route)) {

return null;
}

if (method_exists($route, 'getController')) {

$postmanModelClass = $route->getController()->postmanModel;
if (property_exists($route->getController(), 'postmanModel')) {
$postmanModelClass = $route->getController()->postmanModel;
return new $postmanModelClass();
}
}

$action = $route->getAction();
$controllerAction = explode('@', $action['controller']);
$controllerClass = $controllerAction[0];
$controller = app($controllerClass);

if (!property_exists($controller, 'postmanModel')) {

return null;
}

$postmanModelClass = $controller->postmanModel;

return new $postmanModelClass();
}
}
Loading