Symfony2 (2) - NelmioApiDocBundle自动生成文档(译)

2 篇文章 0 订阅
2 篇文章 0 订阅

文章转自 Zhipeng JIANG 的博客 http://jesusjzp.github.io/blog/2013/08/17/symfony2-generate-doc/ 

NelmioApiDocBundle

NelmioApiDocBundle是一个开源的Symfony组建,能够自动生成每个bundle以及内部每个action的document,并支持sandbox测试,可以手动变量输入,非常方便api的开发。

Github的地址是 https://github.com/nelmio/NelmioApiDocBundle.

如何安装

把下面的代码添加到composer.json文件中

{
    "require": {
        "nelmio/api-doc-bundle": "dev-master"
    }
}

在内核文件中app/AppKernel.php添加相应的bundle信息

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Nelmio\ApiDocBundle\NelmioApiDocBundle(),
    );
}

修改路由配置文件routing.yml,添加路径,以后就可以通过api/doc直接访问

# app/config/routing.yml
NelmioApiDocBundle:
    resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
    prefix:   /api/doc

修改配置文件app/config/config.yml,使其生效:

# app/config/config.yml
nelmio_api_doc: ~

具体在代码中如何添加

我们平时做开发最头疼的一个问题就是如何使得文档能够跟开发保持同步,如果修改文档也像修改代码那样频繁,一定需要花费大量的时间和精力去维护。这就是为什么我推荐NelmioApiDocBundle自动文档生成工具。

所有的API接口都写在代码的annotation中,非常方便。

(由此可见,我们的代码的注释也不一定是完全没有用的,这一点在其他语言里体现的也比较多,比如java。)

ApiDoc() 注释

这个bundle为你的每个controller提供了ApiDoc()annotation

 
 
  1. <?php
  2.  
  3. namespace Your\Namespace;
  4.  
  5. use Nelmio\ApiDocBundle\Annotation\ApiDoc;
  6.  
  7. class YourController extends Controller
  8. {
  9. /**
  10. * This the documentation description of your method, it will appear
  11. * on a specific pane. It will read all the text until the first
  12. * annotation.
  13. *
  14. * @ApiDoc(
  15. * resource=true,
  16. * description="This is a description of your API method",
  17. * filters={
  18. * {"name"="a-filter", "dataType"="integer"},
  19. * {"name"="another-filter", "dataType"="string", "pattern"="(foo|bar) ASC|DESC"}
  20. * }
  21. * )
  22. */
  23. public function getAction()
  24. {
  25. }
  26.  
  27. /**
  28. * @ApiDoc(
  29. * description="Create a new Object",
  30. * input="Your\Namespace\Form\Type\YourType",
  31. * output="Your\Namespace\Class"
  32. * )
  33. */
  34. public function postAction()
  35. {
  36. }
  37. }
  38. ?>

在注释部分,有以下几个属性我们可以使用:

  • section: 允许给资源分组

  • resource: 是否这个函数描述了一个主要资源,默认是否

  • description: 用来描述当前的函数

  • deprecated: 允许把当前函数设置成deprecated,默认是否

  • filters: 一个过滤数组

  • input: the input type associated to the method (currently this supports Form Types, classes with JMS Serializer metadata, and classes with Validation component metadata) useful for POST|PUT methods, either as FQCN or as form type (if it is registered in the form factory in the container).

  • output: the output type associated with the response. Specified and parsed the same way as input.

  • statusCodes: 状态码,如404,500等。比如下面的代码,

 
 
  1. <?php
  2.  
  3. class YourController
  4. {
  5. /**
  6. * @ApiDoc(
  7. * statusCodes={
  8. * 200="Returned when successful",
  9. * 403="Returned when the user is not authorized to say hello",
  10. * 404={
  11. * "Returned when the user is not found",
  12. * "Returned when somehting else is not found"
  13. * }
  14. * }
  15. * )
  16. */
  17. public function myFunction()
  18. {
  19. // ...
  20. }
  21. }

这个bundle也可以通过定义的路由来获取信息,比如requirementspattern, 等等,所以为了最好地使用这个bundle,你应该严格定义每个函数的requirements等信息。

本文翻译自NelmioApiDocBundle的github主页的官方文档


关于symfony2的更多博客请参考 :



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值