Method | URI | Action |
---|---|---|
GET | api/articles | articles.index › ArticleController@index |
POST | api/articles | articles.store › ArticleController@store |
GET | api/articles/{article} | articles.show › ArticleController@show |
PUT | api/articles/{article} | articles.update › ArticleController@update |
DELETE | api/articles/{article} | articles.destroy › ArticleController@destroy |
- Request Line: GET /api/articles
- Request Header: Content-Type: application/json
- Query Parameters: None
- Request Body: None
- Request Example: GET http://localhost:8080/api/articles
- Response: JSON
- Response Example:
[
{
"id": 1,
"title": "Test Title",
"description": "Test Description",
"body": "Test body content"
}
]
- Error Response: Common HTTP errors (404, 500等)
- Error Response Example:
{
"message": "Resource not found."
}
- Request Line: POST /api/articles
- Request Header: Content-Type: application/json
- Query Parameters: None
- Request Body:**
{
"title": "New Title",
"description": "New Description",
"body": "New body content"
}
- Request Example: POST http://localhost:8080/api/articles
- Response: JSON
- Response Example:
{
"article": {
"id": 2,
"title": "New Title",
"description": "New Description",
"body": "New body content",
"created_at": "2024-05-03T06:52:31.000000Z",
"updated_at": "2024-05-03T07:33:13.000000Z"
}
}
- Error Response: Validation error (422)
- Error Response Example:
{
"message": "Validation error occurred.",
"errors": {
"title": [ "The title field is required."]
}
}
- Request Line: GET /api/articles/{slug}
- Request Header: Content-Type: application/json
- Query Parameters: None
- Request Body: None
- Request Example: GET http://localhost:8080/api/articles/example-slug
- Response: JSON
- Response Example:
{
"id": 1,
"title": "Test Title",
"description": "Test Description",
"body": "Test body content",
"created_at": "2024-05-03T06:52:31.000000Z",
"updated_at": "2024-05-03T07:33:13.000000Z"
}
- Error Response: Common HTTP errors (404, 500等)
- Request Body:
{
"message": "Resource not found."
}
- Request Line: PUT /api/articles/{slug}
- Request Header: Content-Type: application/json
- Query Parameters: None
- Request Body:**
{
"title": "Updated Title",
"description": "Updated Description",
"body": "Updated body content"
}
- Request Example: PUT http://localhost:8080/api/articles/example-slug
- Response: JSON
- Response Example:
{
"article": {
"id": 1,
"title": "Updated Title",
"description": "Updated Description",
"body": "Updated body content",
"created_at": "2024-05-03T06:52:31.000000Z",
"updated_at": "2024-05-03T07:33:13.000000Z"
}
}
- Error Response: Validation error (422)
- Error Response Example:
{
"message": "Validation error occurred.",
"errors": {
"title": [ "The title field is required."]
}
}
- Request Line: DELETE /api/articles/{slug}
- Request Header: Content-Type: application/json
- Query Parameters: None
- Request Body: None
- Request Example:
DELETE http://localhost:8080/api/articles/example-slug
- Response: JSON
- Response Example:
{
"message": "success"
}
- Error Response: Common HTTP errors (404, 500等)
- Error Response Example:
{
"message": "Resource not found.",
"exception": {
"message": "No query results for model [App\\Models\\Article] 1",
}
}
-
Requestを使用したバリデーション:
- 目的: リクエストデータの整合性を保証し、不正なデータの処理を防ぐ。
- 詳細:
api\app\Http\Requests\StoreArticleRequest.php
とapi\app\Http\Requests\UpdateArticleRequest.php
を使用して、記事の作成と更新時に必要なデータが適切に提供されているかを検証します。これにより、データベースに無効なデータが保存されるのを防ぎます。
-
リソースの自動解決:
- 目的: コントローラのアクションで直接モデルを操作することで、ルーティングの複雑さを減らし、コードの可読性を向上させる。
- 詳細:
api\routes\api.php
において、{article}
パラメータを使用することで、Laravelのルートモデルバインディング機能が自動的に対応するArticle
モデルのインスタンスを解決し、コントローラメソッドに注入します。
-
エラーハンドリング:
- 目的: アプリケーションの安定性を保ち、ユーザーに適切なフィードバックを提供する。
- 方法:
api\bootstrap\app.php
カスタム例外ハンドラを使用して、特定のHTTPエラー(404, 405など)に対してJSON形式のエラーレスポンスを返します。これにより、クライアント側でのエラー処理が容易になります。
docker-compose up -d
docker-compose exec api bash
php artisan migrate