2012-04-12 18:38:13 +02:00
|
|
|
|
NelmioApiDocBundle
|
|
|
|
|
==================
|
|
|
|
|
|
2012-04-12 19:42:53 +02:00
|
|
|
|
The **NelmioApiDocBundle** bundle allows you to generate a decent documentation for your APIs.
|
|
|
|
|
|
|
|
|
|
|
2012-04-12 19:10:02 +02:00
|
|
|
|
## Installation ##
|
2012-04-12 18:38:13 +02:00
|
|
|
|
|
2012-04-12 19:10:02 +02:00
|
|
|
|
Register the namespace in `app/autoload.php`:
|
|
|
|
|
|
|
|
|
|
// app/autoload.php
|
|
|
|
|
$loader->registerNamespaces(array(
|
|
|
|
|
// ...
|
|
|
|
|
'Nelmio' => __DIR__.'/../vendor/bundles',
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
Register the bundle in `app/AppKernel.php`:
|
|
|
|
|
|
|
|
|
|
// 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"
|
|
|
|
|
prefix: /
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
class YourController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @ApiDoc(
|
|
|
|
|
* resource="true",
|
|
|
|
|
* 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",
|
|
|
|
|
* formType="Your\Namespace\Form\Type\YourType"
|
|
|
|
|
* )
|
|
|
|
|
*/
|
|
|
|
|
public function postAction()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The following parameters are recognized:
|
|
|
|
|
|
|
|
|
|
* `resource`: whether the method describes a resource or not;
|
|
|
|
|
|
|
|
|
|
* `description`: a description of the API method;
|
|
|
|
|
|
|
|
|
|
* `filters`: an array of filters;
|
|
|
|
|
|
|
|
|
|
* `formType`: the Form Type associated to the method, useful for POST|PUT methods.
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
If you set a `formType`, then the bundle automatically extracts parameters based on the given type,
|
|
|
|
|
and determines for each parameter its data type, and if it's required or not.
|
|
|
|
|
|
2012-04-12 19:42:53 +02:00
|
|
|
|
The bundle will also get information from the routing definition (`requirements`, `pattern`, etc).
|
|
|
|
|
|
2012-04-12 19:10:02 +02:00
|
|
|
|
|
|
|
|
|
### Documentation on-the-fly ###
|
|
|
|
|
|
2012-04-12 19:42:53 +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 ###
|
|
|
|
|
|
|
|
|
|
You can browse the whole documentation at: `http://yourproject/api/doc`.
|
|
|
|
|
|
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 ###
|
|
|
|
|
|
|
|
|
|
A command is provided in order to dump the documentation in `json`, `markdown, or `html`.
|
|
|
|
|
|
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`).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Credits ##
|
|
|
|
|
|
|
|
|
|
The design is heavily inspired by the [swagger-ui](https://github.com/wordnik/swagger-ui) project.
|