RESTful API Doc using SLIM and SWAGGER?
Asked Answered
D

3

23

I am new to this stuff but I love it. I made a little REST Api using the Slim Framework. Now I would like to have an ongoing documentation about it. I think swagger is the right choice but I have not found out yet how to integrate it ?

Cheers & thanks for your patience :)

Dumbarton answered 30/4, 2014 at 19:32 Comment(2)
I've heard good thing about Swagger - are there any docs on the associated website? However, my guess is this topic is too broad here - bear in mind that Stack Overflow is for programming problems involving code. If you can give it a go and edit this question about what you are stuck on, that would be great.Goolsby
I dont know how to integrate swagger in my PHP project. They offer a php client on Github and a Server integration tutorial for Java but this didnt take me further. At the moment I have no idea which steps I have to take. I dont want to put comments in my source and then run a tool which create s a documentation.Dumbarton
M
28

I think you are looking for this project: zircote/swagger-php

Here you'll find how to generate the doc on user request to a URL.

Basically you have to annotate your code with the Swagger Annotations, then create another route in Slim with a code similar to this:

<?php
use Swagger\Swagger;
$swagger = new Swagger('/project/root/top_level');
header("Content-Type: application/json")
echo $swagger->getResource('/pet', array('output' => 'json'));

And it will generate the Swagger API docs on the fly for you.

Millett answered 30/4, 2014 at 21:14 Comment(4)
thanks! but how do I integrate the swagger-UI with that then?Dumbarton
got it up and running :)Dumbarton
@Dumbarton Would you be so kind as to explain how you got it up and running?Malevolent
@Malevolent You need to install swagger-ui: github.com/swagger-api/swagger-ui and make it point to the swagger route.Millett
S
6

A short update to the answer of adosaiguas:

When using Slim Framework 4.0 and zircote/swagger-php one can provide an api endpoint providing the swagger / OpenAPI 3.0 json description using the following code:

use function OpenApi\scan;

 /**
 * @OA\Get(
 *     path="/openapi",
 *     tags={"documentation"},
 *     summary="OpenAPI JSON File that describes the API",
 *     @OA\Response(response="200", description="OpenAPI Description File"),
 * )
 */
$app->get('/openapi', function ($request, $response, $args) {
    $swagger = scan('--PATH TO PROJECT ROOT--');
    $response->getBody()->write(json_encode($swagger));
    return $response->withHeader('Content-Type', 'application/json');
});

Stagecraft answered 24/8, 2020 at 11:59 Comment(0)
H
0

When you're searching for a Swagger integration that is fully automatically derived from your PHP code, you can have a look at my latest project: https://github.com/lnaegele/PSwag

This project bases on Slim and provides an OpenAPI 3.0 spec and a swagger page "on the fly" - without any manual generation steps from your side. You just need to specify datatypes to parameters and returns of your API methods.

Example: Create an endpoint function in PetApplicationService.class:

/**
 * Find pet by ID
 * @param int $petId ID of pet to return
 * @return Pet Returns a single pet
 */
public function getPetById(int $petId): Pet {
    return new Pet(...);
}

In your index.php you register the endpoint in PSwagApp which is a wrapper around SlimApp:

// create SlimApp and wrapper PSwagApp
$slimApp = AppFactory::create();    
$app = new PSwagApp($slimApp);

...

// add swagger middleware
$app->addSwaggerUiMiddleware('/swagger', 'PSwag example', '1.0.0', 'vendor/swagger-api/swagger-ui/dist/');

// register endpoints by specifying class and method name
$app->get('/pet/{petId}', [PetApplicationService::class, 'getPetById']);

The result will look like this: Swagger screenshot

Horatia answered 13/4 at 23:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.