php – Symfony – 从未导入注释

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

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

注释的示例是:

/**
 * @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中的路由注释相同.

在生产环境中它工作正常,但在开发环境中我得到例外

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

有没有办法解决这个问题,并告诉Symfony apidoc的注释必须被忽略?

最佳答案 您可以使用IgnoreAnnotation批注告诉Docrine批注阅读器在控制器中跳过此批注.为此,只需将注释添加@IgnoreAnnotation(“Annotation”)添加到类的类doc注释中.

在你的情况下:

/**
 * @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以将此注释包含为默认跳过为this one.

希望这有帮助.

点赞