[Laravel] ๋ผ๋ผ๋ฒจ์— swagger ์ ์šฉํ•˜๊ธฐ

2023. 4. 21. 11:09ยท๐Ÿ“Framework/๐ŸŽต Laravel
๋ชฉ์ฐจ
  1. ๊ธฐ๋ณธ ์„ธํŒ…
  2. ๋‚ด์šฉ ์ž‘์„ฑํ•˜๊ธฐ
  3. laravel bearer_token ์ธ์ฆ
๋ฐ˜์‘ํ˜•

๊ธฐ๋ณธ ์„ธํŒ…

1. ํ”„๋กœ์ ํŠธ์— swagger ์„ค์น˜ํ•˜๊ธฐ

composer require "darkaonline/l5-swagger"

 

2. config ํŒŒ์ผ ์ƒ์„ฑ

php artisan vendor:publish --provider "L5Swagger\L5SwaggerServiceProvider"

 

3. provider ์„ค์ •

config/app.php ํŒŒ์ผ ์ˆ˜์ •

'providers' => [
	/*
    * Laravel Framework Service Providers...*/
    L5Swagger\L5SwaggerServiceProvider::class,
]

 

4. config ์„ค์ •

config/I5-swagger.php ํŒŒ์ผ ์ˆ˜์ •

'api' => [
	'title' => {์›ํ•˜๋Š” ์ œ๋ชฉ}
],
'routes' => [
	/*
    * Route for accessing api documentation interface
    */
    'api' => 'api/documentation',
],
'defaults' => [
	'routes' => [
    	/*
         * Route for accessing parsed swagger annotations.
         */
         'docs' => 'docs',

 

5. ์ปจํŠธ๋กค๋Ÿฌ ํŒŒ์ผ์ค‘ ํ•˜๋‚˜์— Annotation์œผ๋กœ ๊ธฐ์ˆ 

 /**
 * @OA\Info(
 *     title="TEST API",
 *     version="1.0",
 *     description="TEST API Document"
 * )
 **/

 

6. ์ ์šฉ (ํ„ฐ๋ฏธ๋„)

php artisan l5-swagger:generate

 

7. http://127.0.0.1:8000/api/documentation# ์œผ๋กœ ์ ‘์†

 


 

๋‚ด์šฉ ์ž‘์„ฑํ•˜๊ธฐ

- ์ปจํŠธ๋กค๋Ÿฌ ์•ˆ์˜ public function ์œ„์— ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

 

@OA\{Get | Post | Put | Delete}

* @OA\Get (
*     path="/api/v1/test/{id}",
*     operationId="testAPI",
*     tags={"ํ…Œ์ŠคํŠธ"},
*     summary="ํ…Œ์ŠคํŠธ api",
*     description="ํ…Œ์ŠคํŠธ์šฉ ์ž‘์„ฑ",
*     security={{"bearer_token":{}}},
*     @OA\Parameter(
*          name="id",
*          description="ํŒŒ๋ผ๋ฏธํ„ฐ",
*          in="path",
*          required=true,
*          example="test"
*          @OA\Schema(type="string")
*     ),
*     @OA\Response(
*          response="200",
*          description= "์„ฑ๊ณต"
*      ),
*     @OA\Response(
*          response="400",
*          description= "์‹คํŒจ"
*      ),
*     security={{"api_key_security":{}}}
* )

 

@OA\Parameter

- Get ๋ฐฉ์‹ ์ „์†ก

*     @OA\Parameter(
*          name="test_parameter",
*          description="ํŒŒ๋ผ๋ฏธํ„ฐ",
*          in="path",
*          required=true,
*          example="test"
*          @OA\Schema(type="string")
*     )

- Header๋กœ ์ „์†ก

*     @OA\Parameter(
*          name="test_parameter",
*          description="ํŒŒ๋ผ๋ฏธํ„ฐ",
*          in="header",
*          required=true,
*          example="test"
*          @OA\Schema(type="string")
*     )

 

@OA\RequestBody

- jsonํ˜•์‹

*     @OA\RequestBody(
*          @OA\MediaType(e
*              mediaType="application/json",
*              @OA\Schema(
*                  @OA\Property(property="image_file", type="int", description="ํšŒ์› ๋ฒˆํ˜ธ", example=1),
*                  @OA\Property(property="name", type="string", description="ํšŒ์› ์ด๋ฆ„", example="๊น€๊น€๊น€")
*              )
*          )
*      )

- form-data ํ˜•์‹

 *     @OA\RequestBody(
 *          @OA\MediaType(
 *              mediaType="multipart/form-data",
 *              @OA\Schema(
 *                  @OA\Property(property="image_file", type="file", description="์ด๋ฏธ์ง€ ํŒŒ์ผ", format="file")
 *              )
 *          )
 *      )

 

@OA\Response

- ๋ฌธ์ž ์ถœ๋ ฅ

 *     @OA\Response(
 *          response="400",
 *          description= "์‹คํŒจ"
 *      )

- ๋ฐฐ์—ด ์ถœ๋ ฅ

 *     @OA\Response(
 *          response="200",
 *          description= "์„ฑ๊ณต",
 *          @OA\JsonContent(
 *              @OA\Property(property="result", type="array",
 *                  @OA\Items(
 *                      @OA\Property(property="state", type="string", description="์ƒํƒœ", example=true),
 *                      @OA\Property(property="code", type="int", description="์ฝ”๋“œ", example=2000),
 *                      @OA\Property(property="descMsg", type="string", description="๋ฉ”์„ธ์ง€", example="SUCCESS"),
 *                  ),
 *              ),
 *              @OA\Property(property="data", type="string", description="๋ฐ์ดํ„ฐ", example="์ด๋ฏธ์ง€ url"),
 *          )
 *      )

- ๊ฐ์ฒด ์ถœ๋ ฅ

 *     @OA\Response(
 *          response="200",
 *          description= "์„ฑ๊ณต",
 *          @OA\JsonContent(
 *              @OA\Property(property="result", type="object",
 *                      @OA\Property(property="state", type="string", description="์ƒํƒœ", example=true),
 *                      @OA\Property(property="code", type="int", description="์ฝ”๋“œ", example=2000),
 *                      @OA\Property(property="descMsg", type="string", description="๋ฉ”์„ธ์ง€", example="SUCCESS"),
 *              ),
 *              @OA\Property(property="data", type="string", description="๋ฐ์ดํ„ฐ", example="์ด๋ฏธ์ง€ url"),
 *          )
 *      )

 


 

laravel bearer_token ์ธ์ฆ

comfig/I5-swagger.php ์ˆ˜์ •

'securityDefinitions' => [
    'securitySchemes' => [
    	'bearer_token' => [ // Unique name of security
        	'type' => 'apiKey', // Valid values are "basic", "apiKey" or "oauth2".
            'description' => 'Enter token in format (Bearer <token>)',
            'name' => 'Authorization', // The name of the header or query parameter to be used.
            'in' => 'header', // The location of the API key. Valid values are "query" or "header".
        ]
    ]
]

 

@OA\Info ์ž‘์„ฑํ•œ ๊ณณ์— ์ถ”๊ฐ€

 * @OAS\SecurityScheme(
 *      securityScheme="bearer_token",
 *      type="http",
 *      scheme="bearer"
 * )

 

@OA\{Get | Post | Put | Delete} ์•ˆ์— ์ถ”๊ฐ€

 *     security={{"bearer_token":{}}},

- Beareer ์“ฐ๊ณ  ํ† ํฐ ์ž…๋ ฅ

๋ฐ˜์‘ํ˜•
์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'๐Ÿ“Framework > ๐ŸŽต Laravel' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Laravel] ๋ผ๋ผ๋ฒจ์—์„œ ์—‘์…€ ๋‹ค๋ฃจ๊ธฐ  (0) 2023.04.06
[Laravel] Serialization of 'Illuminate\\Http\\UploadedFile' is not allowed ์—๋Ÿฌ  (0) 2023.03.08
[Laravel] Maximum execution time of 60 seconds exceeded ์—๋Ÿฌ  (0) 2022.05.24
[Laravel] layout  (0) 2021.11.17
[Laravel] routes  (0) 2021.11.17
  1. ๊ธฐ๋ณธ ์„ธํŒ…
  2. ๋‚ด์šฉ ์ž‘์„ฑํ•˜๊ธฐ
  3. laravel bearer_token ์ธ์ฆ
'๐Ÿ“Framework/๐ŸŽต Laravel' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [Laravel] ๋ผ๋ผ๋ฒจ์—์„œ ์—‘์…€ ๋‹ค๋ฃจ๊ธฐ
  • [Laravel] Serialization of 'Illuminate\\Http\\UploadedFile' is not allowed ์—๋Ÿฌ
  • [Laravel] Maximum execution time of 60 seconds exceeded ์—๋Ÿฌ
  • [Laravel] layout
yujch
yujch
ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ณต๋ถ€ ๋ฉ”๋ชจ
    ๋ฐ˜์‘ํ˜•
  • yujch
    โœŽRepository
    yujch
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (95)
      • ๐Ÿ“Language (42)
        • ๐ŸŒ HTML (3)
        • ๐ŸŸจ Javascript (4)
        • ๐ŸŸฃ PHP (9)
        • ๐ŸŸข Node.js (6)
        • โ˜• Java (10)
        • โž• Python (9)
        • ๐Ÿ”ต C (1)
      • ๐Ÿ“Framework (11)
        • ๐Ÿƒ Spring (1)
        • ๐ŸŽต Laravel (9)
        • ๐Ÿš„ Express (1)
      • ๐Ÿ“DB (7)
        • ๐Ÿฌ MySQL (6)
        • ๐Ÿ˜ PostgreSQL (1)
      • ๐Ÿ“DevOps (6)
      • ๐Ÿ“Book (2)
      • ๐Ÿ“ETC. (16)
        • โš’๏ธ Tool (11)
        • ๐Ÿชข API Connection (5)
      • ๐Ÿ“์ž๊ฒฉ์ฆ ๊ณต๋ถ€ (10)
        • ๐Ÿ“™ SQLD (7)
        • ๐Ÿ“™ ๋ฆฌ๋ˆ…์Šค๋งˆ์Šคํ„ฐ (3)
      • ๐Ÿ“˜ ๋…ธํŠธ (1)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ๐Ÿ ํ™ˆ
    • ๐Ÿ“ฎ๋ฐฉ๋ช…๋ก
  • ๋งํฌ

    • Github
  • ์ธ๊ธฐ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.0
yujch
[Laravel] ๋ผ๋ผ๋ฒจ์— swagger ์ ์šฉํ•˜๊ธฐ

๊ฐœ์ธ์ •๋ณด

  • ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ
  • ํฌ๋Ÿผ
  • ๋กœ๊ทธ์ธ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”

๋‹จ์ถ•ํ‚ค

๋‚ด ๋ธ”๋กœ๊ทธ

๋‚ด ๋ธ”๋กœ๊ทธ - ๊ด€๋ฆฌ์ž ํ™ˆ ์ „ํ™˜
Q
Q
์ƒˆ ๊ธ€ ์“ฐ๊ธฐ
W
W

๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€

๊ธ€ ์ˆ˜์ • (๊ถŒํ•œ ์žˆ๋Š” ๊ฒฝ์šฐ)
E
E
๋Œ“๊ธ€ ์˜์—ญ์œผ๋กœ ์ด๋™
C
C

๋ชจ๋“  ์˜์—ญ

์ด ํŽ˜์ด์ง€์˜ URL ๋ณต์‚ฌ
S
S
๋งจ ์œ„๋กœ ์ด๋™
T
T
ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ ์ด๋™
H
H
๋‹จ์ถ•ํ‚ค ์•ˆ๋‚ด
Shift + /
โ‡ง + /

* ๋‹จ์ถ•ํ‚ค๋Š” ํ•œ๊ธ€/์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ‹ฐ์Šคํ† ๋ฆฌ ๊ธฐ๋ณธ ๋„๋ฉ”์ธ์—์„œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.