2012-04-12 18:38:13 +02:00
NelmioApiDocBundle
==================
2012-04-14 02:44:05 +03:00
[![Build Status ](https://secure.travis-ci.org/nelmio/NelmioApiDocBundle.png?branch=master )](http://travis-ci.org/nelmio/NelmioApiDocBundle)
2012-04-12 19:42:53 +02:00
The **NelmioApiDocBundle** bundle allows you to generate a decent documentation for your APIs.
2012-05-23 09:44:19 +02:00
**Important:** This bundle is developed in sync with [symfony's repository ](https://github.com/symfony/symfony ).
For Symfony 2.0.x, you need to use the 1.* version of the bundle.
2012-04-12 19:42:53 +02:00
2012-04-12 19:10:02 +02:00
## Installation ##
2012-04-12 18:38:13 +02:00
2012-07-20 17:35:38 +02:00
Add this bundle to your `composer.json` file:
2012-07-13 16:06:01 +02:00
2012-07-20 17:35:38 +02:00
{
"require": {
"nelmio/api-doc-bundle": "dev-master"
}
}
2012-04-12 19:10:02 +02:00
2012-07-20 17:35:38 +02:00
Register the bundle in `app/AppKernel.php` :
2012-04-12 19:10:02 +02:00
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new Nelmio\ApiDocBundle\NelmioApiDocBundle(),
);
}
Import the routing definition in `routing.yml` :
# app/config/routing.yml
NelmioApiDocBundle:
resource: "@NelmioApiDocBundle/Resources/config/routing .yml"
2012-04-13 10:42:31 +02:00
prefix: /api/doc
2012-04-12 19:10:02 +02:00
Enable the bundle's configuration in `app/config/config.yml` :
# app/config/config.yml
nelmio_api_doc: ~
## Usage ##
2012-04-12 19:42:53 +02:00
The main problem with documentation is to keep it up to date. That's why the **NelmioApiDocBundle**
uses introspection a lot. Thanks to an annotation, it's really easy to document an API method.
### The ApiDoc() annotation ###
2012-04-12 19:10:02 +02:00
The bundle provides an `ApiDoc()` annotation for your controllers:
``` php
< ?php
namespace Your\Namespace;
2012-04-13 17:11:58 +03:00
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
2012-04-12 19:10:02 +02:00
class YourController extends Controller
{
/**
2012-07-13 14:07:56 +01:00
* This the documentation description of your method, it will appear
* on a specific pane. It will read all the text until the first
* annotation.
*
2012-04-12 19:10:02 +02:00
* @ApiDoc (
2012-04-12 20:00:26 +02:00
* resource=true,
2012-04-12 19:10:02 +02:00
* description="This is a description of your API method",
* filters={
* {"name"="a-filter", "dataType"="integer"},
* {"name"="another-filter", "dataType"="string", "pattern"="(foo|bar) ASC|DESC"}
* }
* )
*/
public function getAction()
{
}
/**
* @ApiDoc (
* description="Create a new Object",
2012-11-03 00:59:53 +11:00
* input="Your\Namespace\Form\Type\YourType",
2012-11-19 18:21:12 +01:00
* output="Your\Namespace\Class"
2012-04-12 19:10:02 +02:00
* )
*/
public function postAction()
{
}
}
```
2012-04-13 10:40:05 +02:00
The following properties are available:
2012-04-12 19:10:02 +02:00
2013-04-11 00:34:24 +03:00
* `section` : allow to group resources
2012-04-13 10:40:05 +02:00
* `resource` : whether the method describes a main resource or not (default: `false` );
2012-04-12 19:10:02 +02:00
* `description` : a description of the API method;
2013-03-18 08:40:03 +01:00
* `deprecated` : allow to set method as deprecated (default: `false` );
2012-04-12 19:10:02 +02:00
* `filters` : an array of filters;
2012-08-08 10:21:56 -04:00
* `input` : the input type associated to the method, currently this supports Form Types, and classes with JMS Serializer
metadata, useful for POST|PUT methods, either as FQCN or as form type (if it is registered in the form factory in the container)
2012-11-13 04:45:07 +00:00
2012-11-19 18:21:12 +01:00
* `output` : the output type associated with the response. Specified and parsed the same way as `input` .
2012-04-12 19:10:02 +02:00
2012-11-13 04:45:07 +00:00
* `statusCodes` : an array of HTTP status codes and a description of when that status is returned; Example:
``` php
< ?php
class YourController
{
/**
* @ApiDoc (
* statusCodes={
* 200="Returned when successful",
2013-02-20 14:04:03 +01:00
* 403="Returned when the user is not authorized to say hello",
* 404={
* "Returned when the user is not found",
* "Returned when somehting else is not found"
* }
* }
2012-11-13 04:45:07 +00:00
* )
*/
public function myFunction()
{
// ...
}
}
```
2012-04-12 19:42:53 +02:00
Each _filter_ has to define a `name` parameter, but other parameters are free. Filters are often optional
parameters, and you can document them as you want, but keep in mind to be consistent for the whole documentation.
2012-04-12 19:10:02 +02:00
2012-07-24 14:39:43 -04:00
If you set `input` , then the bundle automatically extracts parameters based on the given type,
2012-04-12 19:10:02 +02:00
and determines for each parameter its data type, and if it's required or not.
2012-07-24 14:39:43 -04:00
For Form Types, you can add an extra option named `description` on each field:
2012-04-13 12:17:11 +02:00
2012-08-07 17:50:58 -04:00
For classes parsed with JMS metadata, description will be taken from the properties doc comment, if available.
2012-04-13 12:17:11 +02:00
``` php
< ?php
class YourType extends AbstractType
{
/**
* {@inheritdoc }
*/
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('note', null, array(
'description' => 'this is a note',
));
// ...
}
}
```
2012-04-13 10:40:05 +02:00
The bundle will also get information from the routing definition (`requirements` , `pattern` , etc), so to get the
best out of it you should define strict _method requirements etc.
2012-04-12 19:42:53 +02:00
2012-04-12 19:10:02 +02:00
### Documentation on-the-fly ###
2012-04-13 10:40:05 +02:00
By calling an URL with the parameter `?_doc=1` , you will get the corresponding documentation if available.
2012-04-12 19:10:02 +02:00
### Web Interface ###
2012-04-13 10:40:05 +02:00
You can browse the whole documentation at: `http://example.org/api/doc` .
2012-04-12 19:10:02 +02:00
2012-04-12 19:32:40 +02:00
![](https://github.com/nelmio/NelmioApiDocBundle/raw/master/Resources/doc/webview.png)
2012-04-12 19:10:02 +02:00
2012-04-12 19:42:53 +02:00
![](https://github.com/nelmio/NelmioApiDocBundle/raw/master/Resources/doc/webview2.png)
2012-04-12 19:10:02 +02:00
### Command ###
2012-04-12 20:01:44 +02:00
A command is provided in order to dump the documentation in `json` , `markdown` , or `html` .
2012-04-12 19:10:02 +02:00
2012-04-12 19:32:40 +02:00
php app/console api:doc:dump [--format="..."]
2012-04-12 19:10:02 +02:00
The `--format` option allows to choose the format (default is: `markdown` ).
2012-04-13 11:31:03 +02:00
For example to generate a static version of your documentation you can use:
2012-04-12 20:10:23 +02:00
php app/console api:doc:dump --format=html > api.html
2012-07-20 12:58:14 +03:00
By default, the generated HTML will add the sandbox feature if you didn't disable it in the configuration.
If you want to generate a static version of your documentation without sandbox, use the `--no-sandbox` option.
2012-04-12 19:10:02 +02:00
2012-04-12 20:34:19 +02:00
## Configuration ##
You can specify your own API name:
# app/config/config.yml
nelmio_api_doc:
name: My API
2012-07-18 13:18:26 +02:00
This bundle provides a sandbox mode in order to test API methods. You can
configure this sandbox using the following parameters:
# app/config/config.yml
nelmio_api_doc:
sandbox:
2012-08-10 13:55:35 +02:00
authentication: # default null, if set, the value of the api key is read from the query string and appended to every sandbox api call
name: access_token
2013-04-10 20:23:10 +02:00
delivery: query # query or http_basic are supported
custom_endpoint: true # default false, if true, your user will be able to specify its own endpoint
2012-07-18 13:18:26 +02:00
enabled: true # default: true, you can set this parameter to `false` to disable the sandbox
endpoint: http://sandbox.example.com/ # default: /app_dev.php, use this parameter to define which URL to call through the sandbox
2013-01-08 16:45:51 -08:00
accept_type: application/json # default null, if set, the value is automatically populated as the Accept header
2012-04-12 20:34:19 +02:00
2012-07-24 14:39:43 -04:00
The bundle provides a way to register multiple `input` parsers. The first parser that can handle the specified
input is used, so you can configure their priorities via container tags. Here's an example parser service registration:
2012-11-13 04:45:07 +00:00
2012-07-24 14:39:43 -04:00
#app/config/config .yml
services:
mybundle.api_doc.extractor.custom_parser:
class: MyBundle\Parser\CustomDocParser;
tags:
- {name: nelmio_api_doc.extractor.parser, priority: 2}
2013-04-08 11:54:09 +02:00
You can also define your own motd content (above methods list). All you have to do is add to configuration:
2013-04-08 12:21:50 +02:00
2013-04-08 11:54:09 +02:00
#app/config/config .yml
motd:
template: AcmeApiBundle::Components/motd.html.twig
2013-04-12 17:05:13 +02:00
## Using your own annotations ##
If you have developped your own project-related annotations, and you want to parse them to populate the ApiDoc, you can provide custom handlers as services. You juste have to implements the `Nelmio\ApiDocBundle\Extractor\HandlerInterface` and tag it as `nelmio_api_doc.extractor.handler` .
#app/config/config .yml
services:
mybundle.api_doc.extractor.my_annotation_handler:
class: MyBundle\AnnotationHandler\MyAnnotationHandler;
tags:
- {name: nelmio_api_doc.extractor.handler}
Look at examples in [Handlers ](https://github.com/nelmio/NelmioApiDocBundle/tree/annotation_handlers/Extractor/Handler )
2012-04-12 19:10:02 +02:00
## Credits ##
The design is heavily inspired by the [swagger-ui ](https://github.com/wordnik/swagger-ui ) project.
2013-02-20 14:04:03 +01:00
Some icons from the [Glyphicons ](http://glyphicons.com/ ) library are used to render the documentation.