Skip to content

OBookBook/RealWorld-Laravel11-API-Docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Logo

RealWorld API Endpoints

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

Get Multiple Articles

image

  • 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."
}

Create Article

image

  • 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"
}
{
  "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."]
  }
}

Get Single Article

image

{
  "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."
}

Update Article

image

  • 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"
}
{
  "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."]
  }
}

Delete Article

image

  • 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.phpapi\app\Http\Requests\UpdateArticleRequest.php を使用して、記事の作成と更新時に必要なデータが適切に提供されているかを検証します。これにより、データベースに無効なデータが保存されるのを防ぎます。
  • リソースの自動解決:

    • 目的: コントローラのアクションで直接モデルを操作することで、ルーティングの複雑さを減らし、コードの可読性を向上させる。
    • 詳細: api\routes\api.phpにおいて、{article} パラメータを使用することで、Laravelのルートモデルバインディング機能が自動的に対応する Article モデルのインスタンスを解決し、コントローラメソッドに注入します。
  • エラーハンドリング:

    • 目的: アプリケーションの安定性を保ち、ユーザーに適切なフィードバックを提供する。
    • 方法: api\bootstrap\app.php カスタム例外ハンドラを使用して、特定のHTTPエラー(404, 405など)に対してJSON形式のエラーレスポンスを返します。これにより、クライアント側でのエラー処理が容易になります。

Setup

Environment

docker-compose up -d

phpmyadmin

http://localhost:5000/

Container Shell

docker-compose exec api bash

Migrate

php artisan migrate

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published