Symfony2 ApiDoc配置

很多项目中都有会API供客户端调用,一般情况下我们会出一些文档,但是调试的话就比较麻烦,需要和其他人员沟通,有时候我们服务器端明明没问题,但是客户端就说连接失败,返回不了东西,这样就会反复来查找原因和沟通,如果有一个可以让他们自己输入参数并直接返回结果的页面供他们使用是不是少了很多不必要的麻烦呢?Symfony也是有一个第三方Bundle可以直接使用的,下面我们来看一下具体如何配置?


(1)app/AppKenel.php中注册这个Bundle

// api doc
new Nelmio\ApiDocBundle\NelmioApiDocBundle(),

(2)在app/config/config.yml中增加配置

nelmio_api_doc:
    name: 'API documentation'
    exclude_sections: []
    motd:
        template:             'NelmioApiDocBundle::Components/motd.html.twig'
    request_listener:
        enabled:              true
        parameter:            _doc
    sandbox:
        enabled:              true
        endpoint:             null
        accept_type:          null
        body_format:
            formats:
     
                # Defaults:
                - form
                - json
            default_format:       form # One of "form"; "json"
        request_format:
            formats:
     
                # Defaults:
                json:                application/json
                xml:                 application/xml
            method:               format_param # One of "format_param"; "accept_header"
            default_format:       json
        authentication:
            delivery:             http # Required
            name:                 jiuyi # Required
     
            # Required if http delivery is selected.
            type:                 basic # One of "basic"; "bearer"
            custom_endpoint:      false

(3)app/config/routing.yml中增加路由

xuejiehome_api:
    resource: "@XuejieApiBundle/Controller"
    prefix:   /api

(4)在Controller中增加可访问的路由和apidoc可解析的注解

use Nelmio\ApiDocBundle\Annotation\ApiDoc;;
     
/**
 * @Route("/test")
 * @ApiDoc(
 * description="测试专用",
 * method="get"
 * )
 */
public function testAction()
{
    exit('test');
}
     
 /**
 * @Route("/xuejiehome")
 * @ApiDoc(
 * description="雪洁之家",
 * method="post",
 * parameters={
 *  {"name"="user_id", "dataType"="integer", "required"=true, "description"="id"},
 *  {"name"="username", "dataType"="string", "required"=true, "description"="date"}
 * }
 * )
 */
public function xuejieHomeAction()
{
    ...
    ...
}

(5)输入http://域名/api/doc  查看效果



参考地址:

https://github.com/nelmio/NelmioApiDocBundle/blob/master/Resources/doc/index.md#installation

by 雪洁 2015-12-02 02:41:40 1442 views
我来说几句

相关文章