Getting started with Laravel and Swagger Documentation

Written by garrettv | Published 2018/05/09
Tech Story Tags: technology | software-development | php | front-end-development | computer-science

TLDRvia the TL;DR App

A short tutorial on how to get Swagger running with Laravel.

Documentation is the backbone of an application. It allows developers after you to understand how the application works without having to read through the entire implementation.

Swagger is a framework that allows real-time authorization and endpoint testing for developers and testers alike.

Setup

Let’s start with a barebones application so we’re all literally on the same page.

Composer

First, we’ll need to download composer to make our Laravel application and download any dependencies

https://getcomposer.org/download/

Laravel

Using composer, we can create our new Laravel application

composer create-project --prefer-dist laravel/laravel blog

Local Development Server

In this tutorial I’ll assume you have a basic understanding of editing your hosts file, so we’ll serve our application straight on localhost for time’s sake.

cd blog && php artisan serve

CTRL+C to stop the server.

Swagger

Swagger-php has been the most reliable and straightforward package to use on projects to generate documentation inside of Laravel.

Installation

composer require zircote/swagger-php

Files & Scripts

Swagger requires some information about our application such as it’s hostname, in this case it’s localhost.

It’s important to keep files not directly tied to our application outside of the project root, so lets run some commands

Create development folder and swagger scripts.

mkdir development && cd developmenttouch swagger.sh && chmod +x swagger.shtouch swagger-constants.phptouch swagger-v1.php

Inside our swagger.sh file, lets add a script to generate our documentation file

Add the following inside your swagger-constants.php file

Finally, add this inside your swagger-v1.php file

Script Execution

We can finally run:

./swagger.sh

# Output

Swagger-PHP 2.0.13------------------

-----------------------0 operations documented-----------------------

It works! Unfortunately, we still have some more setup until we can start testing our application’s endpoints.

Endpoint Documentation & Validation

For this tutorial, we’ll create a custom controller called UserController (app/Http/Controllers/Auth/UserController.php).

First, let’s create an endpoint where we can access this function.

Routing

In routes.php (app/Http/routes.php) add:

Route::get('/create', 'UserController@create');

Controller

For the UserController, we’ll use the following code:

We can verify this endpoint works by testing it in our browser:

cd .. && php artisan serve

http://localhost:8000/create?firstname=medium&lastname=reader

# medium reader

Swagger UI

So far we have our swagger documentation script working with it’s respective output. Now we need to add the user interface that will allow us to interact and test our endpoints.

The swagger.sh script generates a subfolder inside the public folder with a file called swagger.json which contains information for the UI about the endpoints and required data to test against.

/app/public|__ /swagger

Installation

In our swagger.sh script you may have noticed we specified which folder we wanted our swagger to output in.

User Interface

To add an interface go to https://github.com/swagger-api/swagger-ui and download the entire folder. Place it inside the swagger folder.

swagger-api/swagger-ui_swagger-ui — Swagger UI is a collection of HTML, Javascript, and CSS assets that dynamically generate beautiful…_github.com

Script customization

Now that we have the user interface files set, we need to edit the file paths for swagger to work correctly.

Our index.html needs to be updated for our localhost server, you can copy and paste directly from here:

Swagger Annotations

Annotations are the format of writing swagger documentation so they can be generated into swagger.json and thus being testable.

Typically, annotations should be placed in your controllers that directly handle endpoints and validation (app/Http/Controllers).

Let’s start by learning what the most important annotation blocks do.

SWG\Get() refers to what kind of HTTP method we’ll use, in this case, we’re fetching data (GET).

path, the route of our endpoint “/create”

SWG\Parameter() we have two parameters needed for our route — firstname and lastname

query, the parameter will be passed through a query string

SWG\Response() which kind of response and code will we return if the data is correct or incorrect.

Put the following Swagger annotations above your create() function and run ./swagger.sh one final time.

/*** @SWG\Get(* path="/create",* description="Return a user's first and last name",* @SWG\Parameter(* name="firstname",* in="query",* type="string",* description="Your first name",* required=true,* ),* @SWG\Parameter(* name="lastname",* in="query",* type="string",* description="Your last name",* required=true,* ),* @SWG\Response(* response=200,* description="OK",* ),* @SWG\Response(* response=422,* description="Missing Data"* )* )*/

Tada!

You should be able to access the final result here.

End result of generating Swagger docs and Swagger UI Integration

By clicking on our route endpoint, we can input the firstname and lastname and execute the GET request

As with most tutorials, there is a lot to Swagger and documentation in general. I recommend you visit the package on Github to get a better understanding on how everything works and the syntax involved in more complex requests.

Thanks for reading!

View the original story at http://garrettvorce.com/laravel-and-swagger-documentation/


Written by garrettv | Senior Full-Stack Engineer in Bangkok, Thailand
Published by HackerNoon on 2018/05/09