2013-05-03 134 views
3

我正在嘗試使用Swagger-PHP生成JSON文件,以便我可以將它與Swagger-UI一起用於自動文檔。Swagger-PHP爲Swagger-UI生成JSON文件

我試圖鏈接: - https://github.com/zircote/swagger-php

我也試過在http://zircote.com/swagger-php/installation.html

但我的硬盤運氣有文件來解決,我無法實現它。

我可以正確安裝作曲家。 Swagger-PHP的安裝包也正確安裝。

但問題是我無法使用/理解他們提供的測試示例。

因此,如果任何人有它的工作,請幫助!

在此先感謝!

+0

嗨,你能提供更多細節嗎?就像你遇到了什麼錯誤。這會有所幫助。 – 2013-10-09 06:34:18

回答

10

你只要把又名註解註釋你的代碼,型號例如:

/** 
* @SWG\Model(
* id="vps", 
* required="['type', 'hostname']", 
* @SWG\Property(name="hostname", type="string"), 
* @SWG\Property(name="label", type="string"), 
* @SWG\Property(name="type", type="string", enum="['vps', 'dedicated']") 
*) 
*/ 
class HostVps extends Host implements ResourceInterface 
{ 
    // ... 
} 

控制器例如:

/** 
* @SWG\Resource(
* basePath="http://skyapi.dev", 
* resourcePath="/vps", 
* @SWG\Api(
* path="/vps", 
* @SWG\Operation(
* method="GET", 
* type="array", 
* summary="Fetch vps lists", 
* nickname="vps/index", 
* @SWG\Parameter(
*  name="expand", 
*  description="Models to expand", 
*  paramType="query", 
*  type="string", 
*  defaultValue="vps,os_template" 
* ) 
* ) 
* ) 
*) 
*/ 
class VpsController extends Controller 
{ 
    // ... 
} 
控制檯

然後:

php swagger.phar ./your-code-source/ -o ./directory-for-output-files 

然後鏈接生成的文件Swagger UI。 這是幫助嗎?

順便提一下,本文檔:http://zircote.com/swagger-php/annotations.html不完整。這是更好地依靠分析器錯誤,例如:

php swagger.phar ./skynode-api/api/ -o ./foo 
Swagger-PHP 0.9.0 
----------------- 
[INFO] Skipping unsupported property: "foo" for @Swagger\Annotations\Property, expecting "name", "description", "type", "format", "items", "uniqueItems", "required", "minimum", "maximum", "enum", "defaultValue", "_partialId", "_partials" in HostVps in /home/kane/some-dir/some-file.php on line 3 

編輯:揚鞭2.0具有相當不錯的規格on GitHub

BTW,考慮使用Swagger Editor創建API規範文件(JSON/YAML)使用在Swagger UI中。導致php文件中的內聯SWG文檔很醜,並且您在IDE中沒有自動完成支持。

+2

如果您可以在註釋中使用Swagger Editor yaml,這樣不會很酷,這樣內聯文檔就不那麼難看了嗎? – redgeoff 2015-09-16 17:49:47

0

我掙扎在這一個最近看到一些差異,並設法得到它的工作,並希望或許快捷別人:

// In the controller 

/** 
* (other swagger stuff like @SWG\Put, etc...) 
* @SWG\Parameter(name="type",in="path",description="project|task", 
*     required=true, type="array",    
*     @SWG\Items(type="string"), 
*     default="project", 
*     enum={"project", "task"}), 
* (next @SWG\Parameter or closing paran, etc) 
*/ 

我以前得到了與枚舉該工作模型和@SWG \ Items引用它,但沒有保存該代碼(我只是在亂搞),我無法恢復它。我甚至看到我以前提出這個問題並接受了答案!但是,以上是我現在能夠正常工作的唯一方式,總比沒有好。