Generates documentation for your REST API from annotations
Find a file
2012-07-05 11:13:15 +02:00
Annotation Applied some php-cs-fixer magic 2012-05-23 00:42:59 +02:00
Command Add license 2012-04-13 11:03:05 +02:00
Controller Add license 2012-04-13 11:03:05 +02:00
DependencyInjection Add: sandbox, to easily try API methods 2012-06-27 09:45:06 +02:00
EventListener Use proper request properties 2012-04-13 15:21:43 +02:00
Extractor Backported the bundle to support Symfony 2.0.x 2012-05-23 00:44:06 +02:00
Form/Extension [Form] Update to sf2.1 2012-05-27 22:56:01 +02:00
Formatter Merge branch '1.0.x' 2012-06-27 10:15:57 +02:00
Parser Added the support of form types defined as services 2012-06-21 00:11:32 +02:00
Resources Merge branch '1.0.x' 2012-07-05 11:13:15 +02:00
Tests Update composer, fix tests 2012-07-05 09:58:25 +02:00
.gitignore Added phpunit.xml to the ignore file 2012-05-23 00:43:18 +02:00
.travis.yml Removed PHP 5.3.2 in travis-ci config 2012-05-23 09:48:42 +02:00
composer.json Update composer, fix tests 2012-07-05 09:58:25 +02:00
LICENSE Add license 2012-04-13 11:03:05 +02:00
NelmioApiDocBundle.php Renamed missing classes 2012-04-12 18:40:20 +02:00
phpunit.xml.dist Ignore vendor in code coverage 2012-04-13 15:21:45 +02:00
README.md Added the support of form types defined as services 2012-06-21 00:11:32 +02:00

NelmioApiDocBundle

Build Status

The NelmioApiDocBundle bundle allows you to generate a decent documentation for your APIs.

Important: This bundle is developed in sync with symfony's repository. For Symfony 2.0.x, you need to use the 1.* version of the bundle.

Installation

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:   /api/doc

Enable the bundle's configuration in app/config/config.yml:

# app/config/config.yml
nelmio_api_doc: ~

Usage

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

The bundle provides an ApiDoc() annotation for your controllers:

<?php

namespace Your\Namespace;

use Nelmio\ApiDocBundle\Annotation\ApiDoc;

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 properties are available:

  • resource: whether the method describes a main resource or not (default: false);

  • 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, either as FQCN or as form type (if it is registered in the form factory in the container)

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.

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.

You can add an extra option named description on each field:

<?php

class YourType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('note', null, array(
            'description' => 'this is a note',
        ));

        // ...
    }
}

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.

Documentation on-the-fly

By calling an URL with the parameter ?_doc=1, you will get the corresponding documentation if available.

Web Interface

You can browse the whole documentation at: http://example.org/api/doc.

Command

A command is provided in order to dump the documentation in json, markdown, or html.

php app/console api:doc:dump [--format="..."]

The --format option allows to choose the format (default is: markdown).

For example to generate a static version of your documentation you can use:

php app/console api:doc:dump --format=html > api.html

Configuration

You can specify your own API name:

# app/config/config.yml
nelmio_api_doc:
    name:   My API

Credits

The design is heavily inspired by the swagger-ui project.