我是新手,需要一些帮助;) 一切都已建立,并且有效..
我的控制器是通过for循环自动设置的,有没有办法
$services = array(
"Users",
"Radios",
"Stations",
"UserAccess",
"Scratchs",
"Members",
"Pages",
);
$api = $this->app["controllers_factory"];
foreach($services as $service) {
$service = strtolower($service);
/**
* @SWG\Resource(basePath="/api/v1",resourcePath="/$service")
*/
/**
* @SWG\Api(
* path="/$service/{id}",
* description="Operations on $service",
* @SWG\Operation(
* method="GET",
* summary="Find a $service",
* nickname="get$service",
* type="array", items="$ref:$service",
* @SWG\ResponseMessage(code=404, message="$service not found"),
* @SWG\ResponseMessage(code=200, message="$service found")
* )
* )
*/
$api->get('/'.$service, $service.".controller:get");
}
swagger输出看起来像这样..
{
"apiVersion": "v1",
"swaggerVersion": "1.2",
"apis": [
{
"path": "/$service",
"description": "Operations on $service"
}
]
}
有没有办法正确地做到这一点?
答案 0 :(得分:1)
不支持Doctrine注释中的变量(swagger-php使用) 您有2个选项(我将使用swagger-php v2注释,但主体也适用于v1.x):
使用"变量"进行一次操作路径(不推荐)
/**
* @SWG\Get(
* path="/{service}/{id}",
* @SWG\Parameter(
* name="service",
* in="path",
* type="string",
* enum={"users","radios","stations","useraccess","scratchs","members","pages"}
* ),
* @SWG\Parameter(name="id",in="path", type="integer"),
* @SWG\Response(response=200, description="Found")
* )
*/
从代码生成注释(推荐)
require_once("vendor/autoload.php");
$swagger = \Swagger\scan('path/to/project');
$services = array(
"Users",
"Radios",
"Stations",
"UserAccess",
"Scratchs",
"Members",
"Pages",
);
$api = $app["controllers_factory"];
foreach($services as $service) {
$name = strtolower($service);
$path = '/'.$name.'/{id}';
$swagger->paths[$path] = new Swagger\Annotations\Path([
'path' => $path,
'get' => new Swagger\Annotations\Get([
'description' => "Find ".$service,
'responses' => [
new Swagger\Annotations\Response(['response' => 200, 'description'=> $service." found"])
]
])
]);
$api->get('/'.$service, $service.".controller:get");
}
$swagger->validate();
echo $swagger;