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

查看:28
本文介绍了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 批注阅读器跳过控制器中的此批注.为此,只需将注释添加 @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天全站免登陆