招摇过市的PHP和CakePHP
我试图使用我的CakePHP项目,但我遇到了一些问题。有人设置了这个吗?你有什么建议吗?我已经通过composer成功安装了swagger php,并将其加载到我的控制器中(见下文)。我试图通过web视图呈现一个规范,但我不太清楚为什么注册表没有被填充,或者是否需要填充 下面是ApiController.php的内部招摇过市的PHP和CakePHP,php,cakephp,swagger,swagger-php,Php,Cakephp,Swagger,Swagger Php,我试图使用我的CakePHP项目,但我遇到了一些问题。有人设置了这个吗?你有什么建议吗?我已经通过composer成功安装了swagger php,并将其加载到我的控制器中(见下文)。我试图通过web视图呈现一个规范,但我不太清楚为什么注册表没有被填充,或者是否需要填充 下面是ApiController.php的内部 use Swagger\Annotations as SWG; use Swagger\Swagger; public function swagger(){ $pat
use Swagger\Annotations as SWG;
use Swagger\Swagger;
public function swagger(){
$path = APP . 'Model'; //Path to the app directory
$swagger = Swagger::discover($path,APP . 'Model/Behavior');
debug($swagger);
//$swagger->jsonEncode($swagger->registry['/api']);
$swagResults = $swagger->registry;
debug($swagResults);
$this->set(array(
'results' => $swagResults,
'_serialize' => 'results'
));
}
结果
object(Swagger\Swagger) {
resourceList => array()
registry => array()
models => array()
[protected] fileList => array(
(int) 0 => '~/Sites/com/sitename-api/app/Model/[ModelName].php',
... All of my models
)
[protected] path => '~/Sites/com/sitename-api/app/Model'
[protected] excludePath => '~/Sites/com/sitename-api/app/Model/Behavior'
[protected] cache => object(Doctrine\Common\Cache\ArrayCache) {
[private] data => array(
'DoctrineNamespaceCacheKey[]' => (int) 1,
'[][1]' => 'a:4:{s:8:"registry";a:0:{}s:6:"models";a:0:{}s:4:"path";N;s:11:"excludePath";N;}',
'[cd9db43f54f6017ba1a20037c1577eb4d2017868][1]' => 'a:4:{s:8:"registry";a:0:{}s:6:"models";a:0:{}s:4:"path";s:56:"~/Sites/com/sitename-api/app/Model";s:11:"excludePath";s:65:"~/Sites/com/sitename-api/app/Model/Behavior";}'
)
}
[protected] cacheKey => 'cd9db43f54f6017ba1a20037c1577eb4d2017868'
}
因此,$swagResults基本上是空的,我猜这不应该是空的,对吗?我编写了一个控制器,生成所有的swagger文档(需要swagger php 0.6或更高版本):
谢谢你的回答,鲍勃,我意识到如何真正排除目录。无论如何,到目前为止,以下是可行的。现在我只需要更好地处理实际的招摇过市规范
在模型中,添加以下内容:
use Swagger\Annotations as SWG;
/**
* User Model
* @SWG\Model(
* id="User",
* description="Defines a user."
* )
*/
在控制器中,添加以下内容。注意:控制器名称是将传递给swagger方法的名称
use Swagger\Annotations as SWG;
/**
* @SWG\Resource(
* resourcePath="/users"
* )
*/
在API控制器中构建如下方法
/**
* swagger method
* This method renders the Swagger spec
* @param string $controller The controller a.k.a resource to pull Swagger docs for
* @return array
*/
public function swagger($controller = ''){
if(!empty($resource)) {
$this->request->query['resource'] = '/'.$controller;
}
$path = APP; //Path to the app directory
$path = substr($path, 0, -1);
$swagger = Swagger::discover(
$path,
APP . 'Plugin:' .
APP . 'Vendor:' .
APP . 'Config:' .
APP . 'Test:' .
APP . 'Console:' .
//APP . 'Model:' .
APP . 'View/Helper:' .
APP . 'Controller/Component:' .
APP . 'webroot:' .
APP . 'tmp:' .
APP . 'index.php:' .
'libs:' .
'plugins:' .
'vendors'
);
$swagger->setDefaultApiVersion(Configure::read('CC.version'));
$swagger->setDefaultBasePath(Configure::read('CC.site_url') . DS . Configure::read('CC.version'));
$swagger->setDefaultSwaggerVersion(SWAGGER_VERSION);
$this->autoRender = false;
if (isset($this->request->query['resource'])) {
return $swagger->getResource($this->request->query['resource']);
}
$list = array(
"apiVersion" => API_VERSION,
"swaggerVersion" => "1.1",
"basePath" => Router::url(array('?' => array('resource' =>'')), true),
"apis" => array()
);
if (isset($this->request->query['resource'])) {
return $swagger->getResource($this->request->query['resource']);
}
$list['apis'][] = $swagger->registry;
$this->set(array(
'results' => $list,
'_serialize' => 'results'
));
}
方法注释可能类似于:
/**
* info method
* This provides an app with basic user information.
* @param int id The user id
* @return array
* @SWG\Api(
* path="/users/info/{user_id}.{format}",
* description="This provides an app with basic user information.",
* @SWG\Operations(
* @SWG\Operation(
* httpMethod="GET",
* summary="User Basic Info",
* notes="",
* responseClass="List[User]",
* nickname="getUserInfo",
* group="users",
* @SWG\Parameters(
* @SWG\Parameter(
* name="format",
* description="The format that the data will be returned in.",
* paramType="path",
* required="true",
* allowMultiple="false",
* dataType="Array",
* @SWG\AllowableValues(
* valueType="LIST",
* values="['json', 'xml']"
* )
* ),
* @SWG\Parameter(
* name="user_id",
* description="The user id",
* paramType="path",
* required="true",
* allowMultiple="false",
* dataType="int"
* ),
* @SWG\Parameter(
* name="client_id",
* description="Your client id",
* paramType="query",
* required="true",
* allowMultiple="false",
* dataType="string",
* threescale_name="client_ids"
* )
* ),
* @SWG\ErrorResponses(
* @SWG\ErrorResponse(
* code="404",
* reason="User not found"
* )
* )
* )
* )
* )
*/
注意:threescale_名称和组是我添加的自定义操作和参数。如果您想使用这些文件,只需将它们添加到zircote/swagger php/library/swagger/Annotations/(Parameter and Operation).php文件中。这些是3scale.net特定的项目
/**
* info method
* This provides an app with basic user information.
* @param int id The user id
* @return array
* @SWG\Api(
* path="/users/info/{user_id}.{format}",
* description="This provides an app with basic user information.",
* @SWG\Operations(
* @SWG\Operation(
* httpMethod="GET",
* summary="User Basic Info",
* notes="",
* responseClass="List[User]",
* nickname="getUserInfo",
* group="users",
* @SWG\Parameters(
* @SWG\Parameter(
* name="format",
* description="The format that the data will be returned in.",
* paramType="path",
* required="true",
* allowMultiple="false",
* dataType="Array",
* @SWG\AllowableValues(
* valueType="LIST",
* values="['json', 'xml']"
* )
* ),
* @SWG\Parameter(
* name="user_id",
* description="The user id",
* paramType="path",
* required="true",
* allowMultiple="false",
* dataType="int"
* ),
* @SWG\Parameter(
* name="client_id",
* description="Your client id",
* paramType="query",
* required="true",
* allowMultiple="false",
* dataType="string",
* threescale_name="client_ids"
* )
* ),
* @SWG\ErrorResponses(
* @SWG\ErrorResponse(
* code="404",
* reason="User not found"
* )
* )
* )
* )
* )
*/