Try   HackMD

CodeIgniter + Swagger

tags: CMRDB, 行雲身份證, 網頁, 筆記

Yi Rong Wu
Tue, Aug 20, 2019

環境: windows、XAMPP

  1. 下載CodeIgniter

  2. 建資料夾YourProject/swagger/

  3. 下載Swagger UI,解壓縮,將dist/資料夾裡的檔案全搬到YourProject/swagger/

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  4. 安裝

    ​composer require zircote/swagger-php:2.*
    
  5. 有需要的話,安裝

    ​composer require darkaonline/l5-swagger
    
  6. 建立Controller(Member)

  7. 加上一些註解 swg annotation
    一份專案裡至少需要一個@SWG\Info的註解

    /**
    ​ * 这里需要一个主`Swagger`定义:
    ​ * @SWG\Swagger(
    ​ *     @SWG\Info(
    ​ *         title="我的`Swagger`API文檔",
    ​ *         version="1.0.0"
    ​ *     )
    ​ * )
    ​ */
    
  8. 加上一些方法和註解

    /**
    ​ * @SWG\Post(
    ​ *     path="/YourProject/index.php/Member/login",
    ​ *     tags={"會員功能"},
    ​ *     summary="登入",
    ​ *     @SWG\Parameter(
    ​ *         in="query",
    ​ *         name="account",
    ​ *         type="string",
    ​ *         description="帳號",
    ​ *         required=true,
    ​ *     ),
    ​ *     @SWG\Parameter(
    ​ *         in="query",
    ​ *         name="password",
    ​ *         type="string",
    ​ *         description="密碼",
    ​ *         required=true,
    ​ *     ),
    ​ *     @SWG\Response(response="default", description="操作成功")
    ​ * )
    ​ *
    ​ */public   function   login()
    ​{
    ​	echo '{"msg":"hello world"}';
    ​}
    
  9. 下指令生成API 文件檔(json格式)
    路徑和檔名可以自行調整

    php [swagger程式位置] [Controller資料夾(需要掃描的資料夾)] -o [api文件生成位置]

    ​php /xampp/htdocs/YourProject/vendor/zircote/swagger-php/bin/swagger /xampp/htdocs/YourProject/application/controllers -o \xampp\htdocs\YourProject\api_doc.json
    
  10. 修改YourProject/swagger/index.html
    改成你的api文件位置
    http://127.0.0.1/YourProject/api_doc.json

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  11. 瀏覽 http://127.0.0.1/Swagger_CodeIgniter/swagger/
    沒更新的話,用無痕瀏覽

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →