2016-07-01 5 views
3

저는 Symfony 프레임 워크를 사용하고 있으며 내 프로젝트의 RESTful API에 자동 문서 엔진을 추가하려고합니다.Symfony - 주석을 가져 오지 않았습니다

일부 검색 후 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에 대한 주석이 심포니에서 라우팅에 대한 주석과 동일합니다. 잘 작동 프로덕션 환경에서 그런데

하지만, 개발 환경에서 나는

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

같은 예외가 무시 될 수있다 apidoc에 대한 그 주석이 문제를 해결하고 심포니에 말을 어떤 방법이 있나요거야?

+0

논의 [여기] (https://github.com/symfony/symfony/issues/17000)도 – Matteo

답변

1

IgnoreAnnotation 주석을 사용하여 Docrine 주석 판독기에 컨트롤러에서이 주석을 건너 뛰도록 지시 할 수 있습니다. 이를 수행하려면 간단히 주석을 @IgnoreAnnotation("Annotation")을 class의 class 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) 
{ 
... 
} 

또한 기본 this one으로 건너로이 주석을 포함하도록 doctrine/annotations 프로젝트에 PR을 열 고려할 수 있습니다.

희망 도움말.

1

Symfony는 doctrine/annotations 패키지를 사용하여 주석을 구문 분석합니다. 블랙리스트에 올라 있지 않은 알려지지 않은 주석을 발견하면 예외를 던집니다.

당신은 추가로 주석을 블랙리스트에 Doctrine docs - Ignoring missing exceptions을 볼 수는 전역 설정 이후

use Doctrine\Common\Annotations\AnnotationReader; 

AnnotationReader::addGlobalIgnoredName('api'); 
AnnotationReader::addGlobalIgnoredName('apiParam'); 
AnnotationReader::addGlobalIgnoredName('apiGroup'); 
AnnotationReader::addGlobalIgnoredName('apiSuccess'); 

나는 응용 프로그램/autoload.php에 넣고 것입니다.

+0

주셔서 감사합니다,하지만 난 당신의 조언 I를 수행 할 때 'PHP 치명적인 오류 :'Doctrine \ Common \ Annotations \ AnnotationReader '클래스가/home/alex/Projects/parcel-search/web-app/app/autoload에서 발견되지 않습니다.php on line 7.' 내가 뭘 놓쳤는가? –

+1

벤더/autoload.php *를 요구하지만'return' 전에 넣어야합니다. – ShiraNai7

-1

당신은 경로 가져올 수 있습니다

사용 Sensio \ 번들 \ FrameworkExtraBundle \ 구성 \ 경로를;

1

주석 컨테이너를 컴파일하는 동안 주석을 읽으므로 compiler pass 동안 apidocs 주석을 무시하는 것이 좋습니다.

예 :

<?php 
namespace YourBundle\DependencyInjection; 

use Doctrine\Common\Annotations\AnnotationReader; 
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; 
use Symfony\Component\DependencyInjection\ContainerBuilder; 

class IgnoreApiDocsAnnotationsPass implements CompilerPassInterface { 
    public function process(ContainerBuilder $container) { 
     AnnotationReader::addGlobalIgnoredName('api'); 
     AnnotationReader::addGlobalIgnoredName('apiDefine'); 
     AnnotationReader::addGlobalIgnoredName('apiDeprecated'); 
     AnnotationReader::addGlobalIgnoredName('apiDescription'); 
     AnnotationReader::addGlobalIgnoredName('apiError'); 
     AnnotationReader::addGlobalIgnoredName('apiErrorExample'); 
     AnnotationReader::addGlobalIgnoredName('apiExample'); 
     AnnotationReader::addGlobalIgnoredName('apiGroup'); 
     AnnotationReader::addGlobalIgnoredName('apiHeader'); 
     AnnotationReader::addGlobalIgnoredName('apiHeaderExample'); 
     AnnotationReader::addGlobalIgnoredName('apiIgnore'); 
     AnnotationReader::addGlobalIgnoredName('apiName'); 
     AnnotationReader::addGlobalIgnoredName('apiParam'); 
     AnnotationReader::addGlobalIgnoredName('apiParamExample'); 
     AnnotationReader::addGlobalIgnoredName('apiPermission'); 
     AnnotationReader::addGlobalIgnoredName('apiPrivate'); 
     AnnotationReader::addGlobalIgnoredName('apiSampleRequest'); 
     AnnotationReader::addGlobalIgnoredName('apiSuccess'); 
     AnnotationReader::addGlobalIgnoredName('apiSuccessExample'); 
     AnnotationReader::addGlobalIgnoredName('apiUse'); 
     AnnotationReader::addGlobalIgnoredName('apiVersion'); 
    } 
} 

은 내가 apidoc docs에서 주석의 전체 목록을 obtainted. 당신은 당신의 번들 컴파일러 패스를 등록해야

<?php 
namespace YourBundle; 

use YourBundle\DependencyInjection\IgnoreApiDocsAnnotationsPass; 
use Symfony\Component\DependencyInjection\ContainerBuilder; 
use Symfony\Component\HttpKernel\Bundle\Bundle; 

class YourBundle extends Bundle { 
    public function build(ContainerBuilder $container) { 
     parent::build($container); 
     $container->addCompilerPass(new IgnoreApiDocsAnnotationsPass()); 
    } 

} 
관련 문제