如何让Symfony 3忽略特定的注释?

8

我正在使用Symfony 3开发API,希望使用apidoc创建文档。Apidoc使用注释来工作:

/**
 * @api {get} /user/:id Request User information
 * @apiName GetUser
 * @apiGroup User
 *
 * @apiParam {Number} id Users unique ID.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */

但是Symfony抛出了一个注释异常:

[语义错误]方法AppBundle\Controller\API\ApiLoginController::loginAction()中的注释"@apiName"从未被导入。您是否忘记为此注释添加"use"语句?

500内部服务器错误-AnnotationException

有没有办法告诉Symfony忽略这些注释?提前感谢。


1
你考虑过使用NelmioApiDocBundle吗?我不确定apidoc是否能与Symfony兼容。 - Alvin Bunk
是的,我知道那个Bundle,如果没有其他选择,我会使用它,但我已经有了apidoc的经验,我喜欢它的结果,而且现在我没有太多时间去学习如何处理另一个Bundle。 - DandyCC
2个回答

13

还有一种方法可以全局忽略注解,具体请参考官方文档。我们不想为每个类都添加注解,因此将其添加到我们的引导程序文件 web/app.php 中。

Doctrine\Common\Annotations\AnnotationReader::addGlobalIgnoredName('your-custom-annotation');

8

你可以使用一个@IgnoreAnnotation的Doctrine注解。尝试这样做:

/**
 * @IgnoreAnnotation("api")
 * @IgnoreAnnotation("apiName")
 * @IgnoreAnnotation("apiGroup")
 * @IgnoreAnnotation("apiParam")
 * @IgnoreAnnotation("apiSuccess")
 */
class SomeController extends Controller{
...
    /**
     * @api {get} /user/:id Request User information
     * @apiName GetUser
     * @apiGroup User
     *
     * @apiParam {Number} id Users unique ID.
     *
     * @apiSuccess {String} firstname Firstname of the User.
     * @apiSuccess {String} lastname  Lastname of the User.
     */

该链接中的文档在下方。

我不得不修改我的原始帖子。你应该在控制器或类的开头放置@IgnoreAnnotation - Alvin Bunk
这正是我在寻找的。谢谢! - DandyCC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接