Swagger 可以用來生成 Laravel API 文件,輕鬆地記錄和分享 API 的資訊,提供了清晰的介面,
讓開發人員更容易了解和使用您的 API。這有助於提高開發效率並確保 API 文檔的一致性

安裝

1
composer require "darkaonline/l5-swagger"

Controller

加入文件版本、標題、描述、授權

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
namespace App\Http\Controllers;

use OpenApi\Annotations as OA;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

/**
* @OA\Info(
* version="1.0.0",
* title="Book-RESTful-API",
* description="Login to obtain a JWT, with bearer authorization",
* @OA\Contact(
* email="demo@example.com"
* ),
* @OA\License(
* name="Apache 2.0",
* url="http://www.apache.org/licenses/LICENSE-2.0.html"
* )
* )
*/

class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

BookController

新增CRUD註解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

namespace App\Http\Controllers;

use App\Models\Book;
use App\Http\Resources\BookResource;
use Illuminate\Http\Request;

class BookController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/

public function __construct()
{
$this->middleware('auth:api')->except(['index', 'show']);
}
/**
* @OA\Get(
* path="/api/books",
* summary="取得最多25筆的書本與評分",
* tags={"Book"},
* @OA\Response(response="200", description="成功",@OA\JsonContent()),
* )
*/
public function index()
{
return BookResource::collection(Book::with('ratings')->paginate(25));
}
/**
* @OA\POST(
* path="/api/books",
* summary="創建書本",
* tags={"Book"},
* @OA\RequestBody(
* required=true,
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* required={"title", "description"},
* @OA\Property(
* property="title",
* type="string",
* description="標題"
* ),
* @OA\Property(
* property="description",
* type="string",
* description="簡介"
* )
* )
* )
* ),
* @OA\Response(
* response=201,
* description="創建成功"
* ),
* @OA\Response(
* response=401,
* description="未驗證"
* ),
* @OA\Response(
* response=404,
* description="資源不存在"
* ),
* security={{ "bearer_token": {} }}
* )
*/

public function store(Request $request)
{
$book = Book::create([
'user_id' => $request->user()->id,
'title' => $request->title,
'description' => $request->description,
]);

return new BookResource($book);
}

/**
* @OA\Get(
* path="/api/books/{bookId}",
* summary="取得書本",
* tags={"Book"},
* @OA\Parameter(
* name="bookId",
* description="Book id",
* required=true,
* in="path",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Response(
* response=200,
* description="成功"
* ),
* @OA\Response(
* response=404,
* description="資源不存在"
* )
* )
*/
public function show(Book $book)
{
return new BookResource($book);
}

/**
* @OA\Patch(
* path="/api/books/{bookId}",
* summary="更新書本",
* tags={"Book"},
* description="更新書本",
* @OA\Parameter(
* name="bookId",
* description="Book id",
* required=true,
* in="path",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\RequestBody(
* description="更新書本的內容",
* required=true,
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* type="object",
* @OA\Property(
* property="title",
* description="標題",
* type="string"
* ),
* @OA\Property(
* property="description",
* description="內容",
* type="string"
* ),
* )
* )
* ),
* @OA\Response(
* response=200,
* description="成功",
* @OA\JsonContent()
* ),
* @OA\Response(
* response=401,
* description="未驗證",
* @OA\JsonContent()
* ),
* @OA\Response(
* response=404,
* description="資源不存在",
* @OA\JsonContent()
* ),
* security={{ "bearer_token": {} }}
* )
*/
public function update(Request $request, Book $book)
{
if ($request->user()->id !== $book->user_id) {
return response()->json(['error' => 'You can only edit your own books.'], 403);
}

$book->update($request->only(['title', 'description']));

return new BookResource($book);
}

/**
* @OA\Delete(
* path="/api/books/{bookId}",
* summary="刪除書本",
* tags={"Book"},
* @OA\Parameter(
* name="bookId",
* description="Book id",
* required=true,
* in="path",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Response(
* response=204,
* description="刪除成功",
* @OA\JsonContent()
* ),
* @OA\Response(
* response=404,
* description="資源不存在",
* @OA\JsonContent()
* ),
* security={{ "bearer_token": {} }}
* )
*/
public function destroy(Book $book)
{
$book->delete();

return response()->json(null, 204);
}
}

生成文件

1
php artisan l5-swagger:generate

訪問以下路由即可看到文件

1
<server>/api/documention

demo

範例在這

Ref

L5-Swagger
swagger-demo
use JWT Bearer token in swagger Laravel