minhngoc / lumen-apidoc-generator
Generate beautiful API documentation using Dingo router from your Lumen application
Requires
- php: >=5.5.0
- dingo/api: 2.0.0-alpha1
- fzaninotto/faker: ~1.0
- illuminate/view: 5.*
- laravel/framework: ~5.4
- mnapoli/front-yaml: ^1.5
- mnapoli/silly: ~1.0
- ramsey/uuid: ^3.0
Requires (Dev)
- mockery/mockery: ^0.9.5
- orchestra/testbench: ~3.0
- phpunit/phpunit: ~4.0 || ~5.0
This package is auto-updated.
Last update: 2024-12-09 12:59:13 UTC
README
Automatically generate your API documentation using Dingo router in your Lumen app. Take a look at the example documentation.
php artisan api:generate --router="dingo" --routePrefix="v1"
Installation
Require this package with composer. In file composer.json add 2 packages:
"minhngoc/lumen-apidoc-generator": "dev-master"
After run command update package:
$ composer update
Go to your bootstrap/app.php
and register the service provider:
$app->instance('path.config', app()->basePath() . DIRECTORY_SEPARATOR . 'config'); $app->instance('path.storage', app()->basePath() . DIRECTORY_SEPARATOR . 'storage'); $app->register(Dingo\Api\Provider\LumenServiceProvider::class); $app->register(Minhngoc\ApiDoc\ApiDocGeneratorServiceProvider::class);
Usage
To generate your API documentation, use the api:generate
artisan command.
$ php artisan api:generate --router="dingo" --routePrefix="v1"
This command will scan your applications routes for the URIs matching v1/*
and will parse these controller methods and validate rules. For example:
$api = app('Dingo\Api\Routing\Router'); $api->version('v1', function ($api) { $api->group(['namespace' => 'App\Http\Controllers'], function ($api) { // Custom get route added to standard Resource $api->get('example/get', 'ExampleController@foo'); //Custom post route added to standard Resource $api->post('example/post', 'ExampleController@save'); }); });
Available command options:
Customisation or translation.
By default, this package returns the descriptions in english. You can publish the packages language files, to customise and translate the documentation output.
$ php artisan vendor:publish
After the files are published you can customise view, styles or translate the descriptions in the language you want by renaming the en
folder and editing the files in public/vendor/apidoc/resources/lang
.
How does it work?
This package uses these resources to generate the API documentation:
Controller doc block
This package uses the HTTP controller doc blocks to create a table of contents and show descriptions for your API methods.
Using @resource
in a doc block prior to each controller is useful as it creates a Group within the API documentation for all methods defined in that controller (rather than listing every method in a single list for all your controllers), but using @resource
is not required. The short description after the @resource
should be unique to allow anchor tags to navigate to this section. A longer description can be included below.
Above each method within the controller you wish to include in your API documentation you should have a doc block. This should include a unique short description as the first entry. An optional second entry can be added with further information. Both descriptions will appear in the API documentation in a different format as shown below.
/** * @resource Example * * Longer description */ class ExampleController extends Controller { /** * This is the short description [and should be unique as anchor tags link to this in navigation menu] * * This can be an optional longer description of your API call, used within the documentation. * */ public function foo(){ }
Result:
Validation rules
To display a list of valid parameters, your API methods accepts, this package uses Lumen Validation Helper Method Validation.
$this->validate($request,[ 'title' => 'required|max:255', 'body' => 'required', 'type' => 'in:foo,bar', 'thumbnail' => 'required_if:type,foo|image', ]);
Controller method doc block
It is possible to override the results for the response. This will also show the responses for other request methods then GET.
@transformer
With the transformer you can define the transformer that is used for the result of the method. It will try the next parts to get a result if it can find the transformer. The first successfull will be used.
- Check if there is a transformermodel tag to define the model
- Get a model from the modelfactory
- If the parameter is a Eloquent model it will load the first from the database.
- A new instance from the class
/** * @transformer \Davmixcool\ApiDoc\Tests\Fixtures\TestTransformer */ public function transformerTag() { return ''; }
@transformercollection
This is the same idea as the @tranformer tag with one different, instead of the return of an item, it will generate the return of a set with two items
/** * @transformercollection \Davmixcool\ApiDoc\Tests\Fixtures\TestTransformer */ public function transformerCollectionTag() { return ''; }
@transformermodel
The @transformermodel tag is needed for PHP 5.* to get the model. For PHP 7 is it optional to specify the model that is used for the transformer.
@response
If you expliciet want to specify the result of a function you can set it in the docblock
/** * @response { * data: [], *} */ public function responseTag() { return ''; }
API responses
If your API route accepts a GET
method, this package tries to call the API route with all middleware disabled to fetch an example API response.
If your API needs an authenticated user, you can use the actAsUserId
option to specify a user ID that will be used for making these API calls:
$ php artisan api:generate --router="dingo" --routePrefix="v1" --actAsUserId=1
If you don't want to automatically perform API response calls, use the noResponseCalls
option.
$ php artisan api:generate --router="dingo" --routePrefix="v1" --noResponseCalls
Note: The example API responses work best with seeded data.
Postman collections
The generator automatically creates a Postman collection file, which you can import to use within your Postman App for even simpler API testing and usage.
If you don't want to create a Postman collection, use the --noPostmanCollection
option, when generating the API documentation.
'url' => env('APP_URL', 'http://yourappdefault.app'),
If you are referring to the environment setting as shown above, then you should ensure that you have updated your .env
file to set the APP_URL value as appropriate. Otherwise the default value (http://yourappdefault.app
) will be used in your Postman collection. Example environment value:
APP_URL=http://yourapp.app
Modify the generated documentation
If you want to modify the content of your generated documentation, go ahead and edit the generated index.md
file.
The default location of this file is: public/docs/source/index.md
.
After editing the markdown file, use the api:update
command to rebuild your documentation as a static HTML file.
$ php artisan api:update
As an optional parameter, you can use --location
to tell the update command where your documentation can be found.
Skip single routes
If you want to skip a single route from a list of routes that match a given prefix, you can use the @hideFromAPIDocumentation
tag on the Controller method you do not want to document.
License
The Lumen API Documentation Generator is free software licensed under the MIT license.