NelmioApiDocBundle/README.md

168 lines
4.4 KiB
Markdown
Raw Normal View History

2016-12-29 13:35:36 +01:00
NelmioApiDocBundle
==================
2016-11-18 18:00:09 +01:00
2016-12-29 13:35:36 +01:00
[![Build
Status](https://secure.travis-ci.org/nelmio/NelmioApiDocBundle.png?branch=master)](http://travis-ci.org/nelmio/NelmioApiDocBundle)
[![Total Downloads](https://poser.pugx.org/nelmio/api-doc-bundle/downloads)](https://packagist.org/packages/nelmio/api-doc-bundle)
[![Latest Stable
Version](https://poser.pugx.org/nelmio/api-doc-bundle/v/stable)](https://packagist.org/packages/nelmio/api-doc-bundle)
The **NelmioApiDocBundle** bundle allows you to generate a decent documentation
for your APIs.
2016-11-18 18:00:09 +01:00
2017-05-13 17:55:09 +02:00
## Migrate from 2.x to 3.0
2017-05-31 14:10:44 +02:00
[To migrate from 2.x to 3.0, just follow our guide.](https://github.com/nelmio/NelmioApiDocBundle/blob/master/UPGRADE-3.0.md)
2017-05-13 17:55:09 +02:00
2016-11-18 18:00:09 +01:00
## Installation
2017-06-22 23:20:12 +02:00
First, open a command console, enter your project directory and execute the following command to download the latest version of this bundle (still in beta, for a stable version look [here](https://github.com/nelmio/NelmioApiDocBundle/tree/2.x)):
2017-05-13 16:39:37 +02:00
2016-11-18 18:00:09 +01:00
```
2017-05-31 14:10:44 +02:00
composer require nelmio/api-doc-bundle dev-master
2016-11-18 18:00:09 +01:00
```
2017-05-13 16:39:37 +02:00
Then add the bundle to your kernel:
2016-11-18 18:00:09 +01:00
```php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = [
// ...
2016-12-29 12:09:26 +01:00
new Nelmio\ApiDocBundle\NelmioApiDocBundle(),
2016-11-18 18:00:09 +01:00
];
// ...
}
}
```
To browse your documentation with Swagger UI, register the following route:
2017-05-13 16:39:37 +02:00
```yml
2017-05-13 17:55:09 +02:00
# app/config/routing.yml
app.swagger_ui:
2017-05-13 16:39:37 +02:00
resource: "@NelmioApiDocBundle/Resources/config/routing/swaggerui.xml"
prefix: /api/doc
```
2016-11-18 18:00:09 +01:00
If you also want to expose it in JSON, register this route:
```yml
# app/config/routing.yml
app.swagger:
path: /api/doc.json
methods: GET
defaults: { _controller: nelmio_api_doc.controller.swagger }
```
2016-11-18 18:00:09 +01:00
## What does this bundle?
It generates you a swagger documentation from your symfony app thanks to
_Describers_. Each of these _Describers_ extract infos from various sources.
2017-05-13 16:39:37 +02:00
For instance, one extract data from SwaggerPHP annotations, one from your
routes, etc.
If you configured the ``app.swagger_ui`` route above, you can browse your
documentation at `http://example.org/api/doc`.
2017-05-13 16:39:37 +02:00
## Configure the bundle
As you just installed the bundle, you'll likely see routes you don't want in
your documentation such as `/_profiler/`. To fix this, you can filter the
routes that are documented by configuring the bundle:
2017-05-13 16:39:37 +02:00
```yml
# app/config/config.yml
nelmio_api_doc:
routes:
path_patterns: # an array of regexps
- ^/api
```
## Use the bundle
You can configure globally your documentation in the config (take a look at
[the Swagger specification](http://swagger.io/specification/) to know the fields
available):
```yml
nelmio_api_doc:
documentation:
info:
title: My App
description: This is an awesome app!
version: 1.0.0
```
To document your routes, you can use annotations in your controllers:
2016-11-18 18:00:09 +01:00
```php
2017-06-14 14:28:37 +02:00
namespace AppBundle\Controller;
2017-05-13 16:39:37 +02:00
use AppBundle\Entity\User;
use AppBundle\Entity\Reward;
use Nelmio\ApiDocBundle\Annotation\Model;
use Swagger\Annotations as SWG;
2017-06-14 14:29:39 +02:00
use Symfony\Component\Routing\Annotation\Route;
2017-05-13 16:39:37 +02:00
2017-06-14 14:28:37 +02:00
class UserController
2017-05-13 16:39:37 +02:00
{
/*
* @Route("/api/{user}/rewards", methods={"GET"})
* @SWG\Response(
* response=200,
* description="Returns the rewards of an user",
* @SWG\Schema(
* type="array",
2017-06-14 14:28:37 +02:00
* @Model(type=Reward::class, groups={"full"})
2017-05-13 16:39:37 +02:00
* )
* )
* @SWG\Parameter(
* name="order",
* in="query",
* type="string",
* description="The field used to order rewards"
* )
* @SWG\Tag(name="rewards")
*/
2017-06-14 14:28:37 +02:00
public function fetchUserRewardsAction(User $user)
2017-05-13 16:39:37 +02:00
{
// ...
}
}
2016-11-18 18:00:09 +01:00
```
## What's supported?
This bundle supports _Symfony_ route requirements, PHP annotations,
[_Swagger-Php_](https://github.com/zircote/swagger-php) annotations,
2017-05-13 16:39:37 +02:00
[_FOSRestBundle_](https://github.com/FriendsOfSymfony/FOSRestBundle) annotations
and apps using [_Api-Platform_](https://github.com/api-platform/api-platform).
2016-11-18 18:00:09 +01:00
2017-05-13 16:39:37 +02:00
It supports models through the ``@Model`` annotation.
2016-12-29 13:35:36 +01:00
## Contributing
See
[CONTRIBUTING](https://github.com/nelmio/NelmioApiDocBundle/blob/master/CONTRIBUTING.md)
file.
## Running the Tests
Install the [Composer](http://getcomposer.org/) dependencies:
git clone https://github.com/nelmio/NelmioApiDocBundle.git
cd NelmioApiDocBundle
composer update
2017-05-13 16:39:37 +02:00
Then run the test suite:
2016-12-29 13:35:36 +01:00
2017-05-13 16:39:37 +02:00
./phpunit
2016-12-29 13:35:36 +01:00
## License
This bundle is released under the MIT license.