This tutorial help to create REST API docs using APIDOc. The apiDoc creates a documentation using API annotations from your PHP source code.There are a lot of application are running into micro services and Rest services.
APIDOC is a npm package that will create API documentation.We are using nodejs and npm for apidoc as a dependency, I am using PHP Laravel API as a source API to create API docs.
The PHP annotations for API Documentation –
/** * @api {get} /employees Request User information * @apiVersion 0.1.0 * @apiName AllEmployee * @apiGroup Employee * * @apiSuccess {String} resp All employee data * @apiError {json} resp The error of about failed rest api. */
You need to add above notation for each method which want to display into apidocs view.
Let’s install apiDoc globally using -g
option:
npm install apidoc -g
Let’s create simple api project to create documentation.
Step 1: Created folder ‘apidocs’ into xampp/htdocs/
.
Step 2: Define apiDoc meta information, we will create apidoc.json
file and added below meta information into this file –
{ "name": "PHPFlow.com", "version": "0.1.0", "description": "Employee API Docs", "apidoc": { "title": "PHPFlow.com", "url" : "https://phpflow.com/" } }
Step 3: Created two folder into ‘apidocs’ folder, The folder name is /source and others is /apidoc. The /source folder will have all .php
files and /apidoc folder will have generated API docs files.
Step 4: Created a sample ApiController.php
file and saved into /source folder, Added below code into this file –
namespace App\Http\Controllers; use Log; use Illuminate\Http\Request; use Employee\Helpers\Helper; use Validator; class ApiController extends Controller { /** * @api {post} /create Add record into employee table * @apiName create * @apiGroup Employee * * @apiParam {json} params required To add item into employee. * * @apiSuccess {json} Results Added employee information. * @apiError {json} Results Failed information. */ public function create(Request $request) { $response = array(); $parameters = $request->json()->all(); try { //save data into mysql db } catch(Exception $ex) { Log::info('Unable to add data'); Log::critical($ex); return Helper::jsonpError("Unable to add data", 400, 400); } } /** * @api {get} employee/:id Get employee information * @apiName getEmployee * @apiGroup Employee * * @apiParam {Number} id required The id of employee. * * @apiSuccess {json} Results The employee information. * @apiError {json} Results Unable to get employee information. */ public function getEmployee($id) { try { //get employee based on path id param } catch(Exception $ex) { Log::info('Unable to get employee information'); Log::critical($ex); return Helper::jsonpError("Unable to get employee information", 400, 400); } } }
You can generate API documentation using below command –
apidoc -i source/ -o apidoc/
Above command will create API docs structure into apidoc/
folder, We can change source path as per our need.
Now open http://localhost/apidocs/doc/
location path into browser.
You can also enable API Tryout feature using @apiSampleRequest annotation.
* @apiSampleRequest /employee/:id
You need to add base URi(http://dummy-api/api/v1
) into apidoc.json
file.
After any changes into annotation or meta information, You need to regenerate api docs using command.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More