Hackernoon logoGetting started with Laravel and Swagger Documentation by@garrettv

Getting started with Laravel and Swagger Documentation

Author profile picture

@garrettvGarrett Vorce

Senior Full-Stack Engineer in Bangkok, Thailand

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

Image created by Pem Saenapatumpan

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.


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


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


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-php has been the most reliable and straightforward package to use on projects to generate documentation inside of Laravel.


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 development
touch swagger.sh && chmod +x swagger.sh
touch swagger-constants.php
touch 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:

# 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.


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

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


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


# 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.

|__ /swagger


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.

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.

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


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/


The Noonification banner

Subscribe to get your daily round-up of top tech stories!