Write API Document for project using Laravel

Tram Ho

Hi everybody. I am currently developing a project that combines Laravel API and reactjs and makes it easy to coordinate between the frontend and backend teams and needs project documents to easily maintain, develop or hand over the project to The next development team should have a project to write documents for the API, after a while of learning and applying, I would like to share some knowledge with everyone. Regarding writing documents for laravel API, I use packet Laravel API Documentation Generator

first . Introducing the package and installation

Laravel API Documentation Generator is a generate API Document package that supports laravel. The Packege currently has nearly 2500star and more than 400 forks and is still under development. This package requires Laravel> = 5.7 and PHP> = 7.2 To install into the Laravel project, run the composer command:

Setting

Run the command to install the library
composer require --dev mpociot/laravel-apidoc-generator
Publish the configuration file by running the command:
php artisan vendor:publish --provider="MpociotApiDocApiDocGeneratorServiceProvider" --tag=apidoc-config
The configuration file of the package will be located in /config/apidoc.php
Run the following command to create API docs
php artisan apidoc:generate
So we have just created the default API docs to be able to customize and write more details for the API docs we will edit as follows

Configuration

The configuration file of the package will be located in /config/apidoc.php. The explanation of this config is as follows:
type: The document type, if selected as static, the document will be an HTML file located in public / docs, if selected laravel, the document will be a blade file located in resources / views / apidoc.
route: to fix it as laravel
base_url: base API URL, default is config (‘app.url’)
postman: install post man collection
enabled: the default is true. will create postman collection
name: top of collection
description: description for the collection
strategies: services to parse API docs
logo: API docs logo, standard size is 230 x 52
default_group: The default group of endpoints does not have the @group attribute
example_languages: programming language for examples
fractal: learn more at: https://fractal.thephpleague.com
routes: includes groups for setting API documents

Write the Document API

To write an API document, go to the method in the controller that the route needs to write the document pointing to. And write above that method as I write below for the index method of the list page of the user list:

The first line is the name of the API. Here is api / users
The following lines describe the param, response, message …

Result

After configuring and writing the API, we run the command:
$ php artisan apidoc:generate and then revisit http: // localhost: 8001 / docs / index.html to see the results:
❤️ Thank you everyone, have any comments on the article, please comment below.

Share the news now

Source : Viblo