despark / apidoc by astoimenov

Generate api documentation and use swagger to consume it
2,333
13
6
Package Data
Maintainer Username: astoimenov
Maintainer Contact: panayot@despark.com (Panayot Balkandzhiyski)
Package Create Date: 2017-01-04
Package Last Update: 2018-08-01
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-21 03:00:35
Package Statistics
Total Downloads: 2,333
Monthly Downloads: 1
Daily Downloads: 0
Total Stars: 13
Total Watchers: 6
Total Forks: 4
Total Open Issues: 4

apidoc

Laravel 5 api documentation generator, based on Swagger

apidoc use just a few lines of code added to your controllers methods.

Installation

Require this package with composer using the following command:

composer require despark/apidoc
 

After that add to the providers array in config/app.php

Despark\Apidoc\ApiDocServiceProvider::class,

Then call

php artisan vendor:publish

Now you are ready to use the generator.

Usage

If you do all steps mentioned above than the file /yourapp/config/apidoc.php should be generated for you.

<?php
return [
    'apiVersion'     => '1.0.0',
    'apiTitle'       => 'My api',
    'apiDescription' => 'My api',
    'apiBasePath'    => '/api/v1',
    'authorization' => 'jwt'
];

All those parameters are displayed on swagger api doc page so you can change them to fit your settings.

Authorization

By default swagger is configured to use JWT authentication via Authorization Header: Bearer. If you want standard auth trough query just change the authorization option to null.

For now these are the only authorization options available.

Controllers and methods

Every single method that has been documented in apidoc documentation way and present in laravel's routs.php will be parsed and shown in the api documentation. Method documentation example:

    /**
     * @apiDesc A description of the method
     * @apiParam string $parameterName required in_path | Description of the parameterName  
     * @apiParam array $parameterName2 | Description2 of the parameterName
     *
     * @apiErr 422 | Validation errors
     * @apiErr 403 | Unauthorized access
     * @apiResp 200 | Whatever message is send from backend on sucess
     */
    public function index($id, DesignRequest $request){}

Notice: Every single "@api" element and description should be on a single row.

  • @apiDesc - A description of the method

  • @apiParam - There is no limit of parameters. Parameters can be required or not required. If word required is typed the parameter is marked as required. Check the example above.

    Params types:

    • string
    • file - uploadable file
    • array
    • bool or boolean (both are available and equal)
    • integer
    • password
    • double
  • @apiErr XXX - can have more than one error messages that are returned in response. XXX is the http status code.

  • @apiResp XXX - the response data of the api call. XXX is the http status code.

  • url parameters (such id in the example), are taken automatically and declared as integer

You can set how the parameters are send and there are 3 options:

  • formData - this is the default option and it is not needed to be mention in the comments
  • in_path - where the parameter value is actually part of the operation's URL. For example, in /items/{itemId}, the path parameter is itemId.
  • in_query - Parameters that are appended to the URL. For example, in /items?id=123, the query parameter is id.
  • more information at Swagger specification page

NOTICE: Everything after "|" symbol is assumed as a description text. So use just one "|" symbol on a row.

Command

After everything is setup, the controllers are declared in route.php file and there are comments in the controllers we can call the command.

php artisan apidoc:generate

That's it. Now you can access your new documentation at APP_URL/docs#/

Notice APP_URL is used for swagger integration. If it is not set in your .env file, the command will return a message, asking you to set up APP_URL.
Notice APP_URL example: http://example.info/