blasttech / laravel-apidoc-generator
Generate beautiful API documentation from your Laravel application
Requires
- php: >=5.5.0
- fzaninotto/faker: ~1.0
- laravel/framework: ~5.4|~5.5.0|~5.6.0|~5.7.0|~5.8.0
- mpociot/documentarian: ^0.2.0
- mpociot/reflection-docblock: ^1.0
- ramsey/uuid: ^3.0
Requires (Dev)
- dingo/api: 1.0.*@dev
- mockery/mockery: ^0.9.5
- orchestra/testbench: ~3.0
- phpunit/phpunit: ~4.0 || ~5.0
- dev-master
- 1.7.5
- 1.7.4
- 1.7.2
- 1.7.1
- 1.7
- 1.6.13
- 1.6.12.1
- 1.6.12
- 1.6.11
- 1.6.10.2
- 1.6.10.1
- 1.6.10
- 1.6.9
- 1.6.8
- 1.6.7.1
- 1.6.7
- 1.6.6.4
- 1.6.6.3
- 1.6.6.2
- 1.6.6.1
- 1.6.6
- 1.6.5
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.1
- 1.0.0
- 0.2.0
- 0.1.0
- dev-bindings
- dev-fix-getMethods
This package is auto-updated.
Last update: 2024-12-18 18:54:18 UTC
README
Automatically generate your API documentation from your existing Laravel routes. A fork of the original laravel-apidoc-generator by Marcel Pociot. Take a look at his example documentation.
php artisan api:gen --routePrefix="settings/api/*"
Installation
Require this package with composer using the following command:
$ composer require mpociot/laravel-apidoc-generator
Go to your config/app.php
and add the service provider:
Blasttech\ApiDoc\ApiDocGeneratorServiceProvider::class,
This package uses two extra validation rules in Laravel. If you wish to use these, you need to add the following to your AppServiceProvider.php
public function boot() { Validator::extend('description', function ($field, $value, $parameters) { return true; }); Validator::extend('faker', function ($field, $value, $parameters) { return true; }); }
Using Laravel < 5.4? Use version 1.0! For Laravel 5.4 and up, use 2.0 instead.
Usage
To generate your API documentation, use the api:generate
artisan command.
$ php artisan api:generate --routePrefix="api/v1/*"
This command will scan your applications routes for the URIs matching api/v1/*
and will parse these controller methods and form requests. For example:
// API Group Routes Route::group(array('prefix' => 'api/v1', 'middleware' => []), function () { // Custom route added to standard Resource Route::get('example/foo', 'ExampleController@foo'); // Standard Resource route Route::resource('example', 'ExampleController')); });
Available command options:
Publish rule descriptions for 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 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:
Form request validation rules
To display a list of valid parameters, your API methods accepts, this package uses Laravels Form Requests Validation.
public function rules() { return [ 'title' => 'required|max:255', 'body' => 'required', 'type' => 'in:foo,bar', 'thumbnail' => 'required_if:type,foo|image', ]; }
As well as the standard rules above, descriptions can be extended in the Description column by adding a description rule, like below:
public function rules() { return [ 'title' => 'required|max:255|description:The book\'s title.', 'body' => 'required|description:Should be a full description of the book.', ]; }
Also, you can set a rule to force a certain faker type to be used in the example requests which are generated. For example:
public function rules() { return [ 'phone_number' => 'description:The user\'s phone number|faker:phoneNumber', 'password' => 'required|description:The user\'s password|faker:password', ]; }
Note: If you use the description or faker rules, make sure you modify your AppServiceProvider.php as shown above.
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 --routePrefix="api/*" --actAsUserId=1
If you don't want to automatically perform API response calls, use the noResponseCalls
option.
$ php artisan api:generate --routePrefix="api/*" --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.
As of as of Laravel 5.3, the default base URL added to the Postman collection will be that found in your Laravel config/app.php
file. This will likely be http://localhost
. If you wish to change this setting you can directly update the url or link this config value to your environment file to make it more flexible (as shown below):
'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.
Further modification
This package uses Documentarian to generate the API documentation. If you want to modify the CSS files of your documentation, or simply want to learn more about what is possible, take a look at the Documentarian guide.
License
The Laravel API Documentation Generator is free software licensed under the MIT license.