Symfony-注释从未导入 [英] Symfony - The annotation was never imported

查看:100
本文介绍了Symfony-注释从未导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Symfony框架,并打算将自动文档引擎添加到我项目的RESTful api中.

I'm using Symfony framework and have intention do add auto-documentation engine to RESTful api of my project.

经过一番搜索,我发现apidoc引擎( http://apidocjs.com/).它的工作非常简单:您必须为RESTful api的每个控制器添加一些批注,然后会生成文档.

After some searching I've found apidoc engine (http://apidocjs.com/). It works pretty simple: you have to add some annotations for every controller of you RESTful api and documentation will generated.

注释的示例是:

/**
 * @Route("/api/dictionary_list/{userId}/{sessionKey}", name="api/dictionary_list")
 * @api {get} /api/dictionary_list/{userId}/{sessionKey} 01. Values list (ids) for all system dictionaries
 * @apiName Dictionary list
 * @apiGroup Dictionary
 *
 * @apiParam {Integer} userId  User's ID received in authorization request
 * @apiParam {String} sessionKey  Session key received in authorization request
 *
 * @apiSuccess {Integer} parcelStatuses  The name of current dictionary
 * @apiSuccess {String} itemId  Item id which used in requests
 * @apiSuccess {String} itemName  Item name
 */

public function dictionaryListAction($userId=null, $sessionKey=null)
{
 ...
}

如您所见,apidoc的注释与Symfony中的路由注释相同.

As you can see, annotation for apidoc is the same as the annotation for routing in Symfony.

顺便说一下,在生产环境中它可以正常工作,但是在开发环境中,我会遇到类似

By the way in production environment it works fine, but in development environment I get exception like

[Semantical Error] The annotation "@apiName" in method AppBundle\Controller\Api\apiDictionaryController::dictionaryListAction() was never imported.

有什么办法可以解决此问题,并向Symfony说必须忽略apidoc的注释?

Is there any way to fix this issue and say to Symfony that annotation for apidoc have to be ignored?

推荐答案

您可以使用IgnoreAnnotation批注告诉Docrine批注阅读器在控制器中跳过此批注.为此,只需在class的类文档注释中添加添加@IgnoreAnnotation("Annotation")的注释即可.

You can use the IgnoreAnnotation annotation to tell Docrine annotation reader to skip this annotation in your controller. To do this, simply put the annotation add @IgnoreAnnotation("Annotation") to the class doc comment of class.

在这种情况下:

/**
 * @IgnoreAnnotation("apiName")
 * @IgnoreAnnotation("apiGroup")
 * @IgnoreAnnotation("apiParam")
 * @IgnoreAnnotation("apiSuccess")
 */
class ActionController extends Controller


/**
 * @Route("/api/dictionary_list/{userId}/{sessionKey}", name="api/dictionary_list")
 * @api {get} /api/dictionary_list/{userId}/{sessionKey} 01. Values list (ids) for all system dictionaries
 * @apiName Dictionary list
 * @apiGroup Dictionary
 *
 * @apiParam {Integer} userId  User's ID received in authorization request
 * @apiParam {String} sessionKey  Session key received in authorization request
 *
 * @apiSuccess {Integer} parcelStatuses  The name of current dictionary
 * @apiSuccess {String} itemId  Item id which used in requests
 * @apiSuccess {String} itemName  Item name
 */

public function dictionaryListAction($userId=null, $sessionKey=null)
{
 ...
}

您还可以考虑打开 doctrine/annotations 项目的PR,以将该注释作为默认值跳过为这一个.

You could also consider to open a PR to the doctrine/annotations project to include this annotation as default skipped as this one.

希望获得帮助.

这篇关于Symfony-注释从未导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆