Skip to main content

47 docs tagged with "laravel"

View all tags

A Practical Guide to Laravel × DDD × Clean Architecture

No Laravel experience required. A 20-chapter guide for SPA developers (Vue/React) that explains Laravel as an API backend together with domain-driven design (DDD) and clean architecture, step by step. It covers the theory of value objects, entities, aggregates, and repositories, through to implementation patterns, transactions, testing, and authentication in a Laravel project.

Conclusion and Next Steps — Introducing DDD Incrementally

Organizes the key points of this guide's introduction and all four parts (DDD tactical design / clean architecture / practical techniques / a comprehensive hands-on example), and summarizes a roadmap for incremental adoption—value objects → use case layer → repository → domain events—along with frequently asked questions and recommended books, communities, and learning paths.

Designing the Use Case Layer — Command / Query Separation and DTOs

Explains the design of the application layer (use case layer). Covers its responsibilities of coordinating domain objects, managing transactions, and converting input/output; Command / Query separation based on the CQS principle; Command objects and DTOs; dedicated Query services; and design guidelines such as single responsibility.

Domain Events — Loosely Coupling Aggregates

Explains the DDD domain event (Domain Event) with implementation examples such as OrderConfirmed. It covers loose coupling through event-driven design, the two-phase implementation of recording and dispatching, integration with Laravel Events, synchronous vs asynchronous (ShouldQueue), and the design of idempotency and eventual consistency.

Domain Model and Table Design — Mapping and Indexes

Covers why to design domain-model-first, mapping patterns under the premise that the domain model and tables are not 1:1 (embedding / one aggregate across multiple tables / technical splitting), how the repository absorbs the gap, and index design including the leftmost-prefix rule for composite indexes and its trade-offs.

Error Handling — Mapping Domain Exceptions to HTTP Statuses

Explains error handling in a DDD architecture. Covers an exception hierarchy organized per aggregate, the design of DomainException, mapping to HTTP statuses (404/409/422, etc.) in the Exception Handler, a unified error response, the Result type pattern, a logging strategy, and protecting sensitive information.

Getting Started — A Practical Guide to Laravel × DDD × Clean Architecture

An introduction to DDD and clean architecture with Laravel, aimed at developers experienced with SPA development (Vue/React). It covers what you will learn, the e-commerce order system used as the running example, the SPA + API architecture assumed throughout, an overview of DDD and clean architecture, and the books to read alongside.

In Practice: Implementing the Order System — An Example That Cuts Across All Layers

Integrates the DDD and clean architecture learned so far, implementing the 4 layers (presentation / application / domain / infrastructure) using the order creation and confirmation APIs as the subject. Summarizes each layer's responsibilities, the flow of data transformation, and layered tests (domain / UseCase / Repository) to bring the whole picture together.

Laravel API 開発ガイド

Laravel を API バックエンドとして使うための全 22 章のガイド。ルーティング・Eloquent・バリデーションの基礎から、認証認可 (Sanctum / Policy)・キャッシュ・キュー・タスクスケジューラ・ファイルストレージ・本番運用までを、EC サイトの注文システムを題材に順を追って解説します。Blade などのフロントエンド機能は扱いません。

PHPクラス設計ガイド

PHP でクラス設計そのものを入門から実践まで学ぶ全14章のガイド。フレームワーク非依存の設計原則 (カプセル化・単一責任・継承とコンポジション・インターフェース・SOLID) から、Laravel でのモデル/サービスクラス設計まで一気通貫で扱います。

Testing Strategy — The Test Pyramid and Layered Testing

Explains the testing strategy for a DDD architecture. Covers the idea of the test pyramid (Unit / Integration / Feature), the benefit of testing the domain layer fast without a DB, minimal use of mocks, Arrange-Act-Assert, descriptive test method names, and the test data builder pattern.

The Repository Pattern — The Boundary Between Domain and Persistence

Implements the repository pattern with Laravel + Eloquent. Covers why the interface lives in the domain layer (DIP / testability), one aggregate = one repository, mapping between entities and Eloquent Models, the N+1 problem and eager loading, mass-assignment protection, and registration in the DI container.

Transaction Management — Where to Place the Consistency Boundary

Explains transaction management using Laravel's DB::transaction(). Covers the ACID properties, when to place transactions inside the Repository (single aggregate) versus the UseCase (multiple aggregates), nested transactions (savepoints), and the implementation and use cases of optimistic and pessimistic locking.

Why DDD — The Limits of the Fat Controller / Fat Model

Organizes the problems that arise when handling complex business logic in traditional Laravel MVC—Fat Controller, Fat Model, and scattered logic—and explains the four benefits gained by adopting DDD: clarifying business rules, resilience to change, testability, and team communication.

イベントと通知 — 注文確定から先を疎結合にする

Laravel API 開発ガイドの第15章。サービスクラスに処理が足され続ける問題をイベントとリスナーで解きます。リスナーの自動検出、Notification クラスと Mailable、同期送信のコストの測り方、Notification::fake によるテストと Mail::fake との使い分けも扱います。

エラー設計 — 例外を HTTP レスポンスに翻訳する

Laravel API 開発ガイドの第9章。エラーレスポンスの形を決め、カスタム例外クラスを作り、bootstrap/app.php の withExceptions を唯一の変換地点にします。何をログに送るか、何をクライアントに見せないかも扱います。

キャッシュ — 速くするために、正しさをどこまで諦めるか

Laravel API 開発ガイドの第18章。何をキャッシュしてよいかの判断、Cache::remember とドライバ選択、キー設計、TTL と明示的な失効、スタンピード対策 (Cache::flexible / Cache::lock)、Laravel 13 の Cache::touch、キャッシュを使うテストの書き方を扱います。

キューの基礎 — 重い処理をリクエストの外に出す

Laravel API 開発ガイドの第16章。リスナーに ShouldQueue を付けて非同期にし、queue:work でワーカーを動かします。Job クラスと dispatch、ドライバの選び方、SerializesModels、トランザクション内で dispatch しないための afterCommit、Queue::route、Queue::fake によるテストも扱います。

タスクスケジューラ — 定期処理を cron 1 行に集約する

Laravel API 開発ガイドの第20章。routes/console.php でのスケジュール定義、cron エントリを 1 行に保つ意味、期限切れトークンと負の在庫と孤児ファイルの掃除、未決済注文の自動キャンセルと「未決済」の判定、withoutOverlapping と onOneServer、travelTo による時刻固定テストと配線テストを扱います。

テスト — Pest で API の契約を固定する

Laravel API 開発ガイドの第10章。Pest の書き方を導入し、ここまでに作った API に Feature テストを書きます。正常系だけでなく失敗パスを固定する理由、テストデータの用意、そして以降の章で守るテストの型を決めます。

はじめに — 何を作り、何を作らないか

Laravel API 開発ガイドの第1章。チュートリアルどおりに書いた注文 API が本番で壊れる 3 つの場面を示し、この連載が扱う範囲と題材、既存ガイドとの読み分けを整理します。

ファイルストレージ — 商品画像と領収書 PDF

Laravel API 開発ガイドの第19章。ディスクという抽象と local / public の使い分け、アップロードの検証と保存、Sanctum の ability による保護、非公開ファイルの配信と署名付き URL、ジョブでの PDF 生成、ファイルとレコードの整合、Storage::fake によるテストを扱います。

まとめ — 本番リリース前チェックリスト

Laravel API 開発ガイドの最終章。21 章分で決めたことを、本番へ出す前に確認する 1 枚のリストにまとめます。設定・データベース・入出力・認可・非同期処理・ログ・デプロイの分野別チェックリストと、どの章で何を決めたかの索引を置きます。

リクエストライフサイクルと Laravel 13 の骨格

Laravel API 開発ガイドの第2章。1 つのリクエストが public/index.php からコントローラまで届く経路を追い、bootstrap/app.php・サービスコンテナ・サービスプロバイダ・設定ファイルがその経路のどこに刺さるかを整理します。

ルーティング設計と最初のエンドポイント

Laravel API 開発ガイドの第4章。install:api で API 用の骨格を作り、apiResource でルートをまとめ、ルートモデルバインディングで ID をモデルに変換します。URL 設計・バージョニング・ステータスコードの決め方も扱います。

ログ・監視とデプロイ — 何が起きたかを後から追えるようにする

Laravel API 開発ガイドの第21章。config/logging.php のチャンネルと stack、Context によるリクエスト ID の伝播、想定内と想定外のログの振り分け、404 と 429 の記録、Monolog プロセッサによる機密情報のマスク、構造化ログ、デプロイ手順の順序、スケジューラの死活監視を扱います。

一覧 API — ページネーション・フィルタ・N+1

Laravel API 開発ガイドの第8章。商品一覧 API を題材に、ページネーション 3 種の使い分け、フィルタとソートを安全に受け取る方法、N+1 問題の観測と eager loading による解決、そして機械的に検出する仕組みを扱います。

入力の検証 — Form Request で境界を固める

Laravel API 開発ガイドの第5章。注文作成 API を題材に、コントローラ直書きのバリデーションを Form Request へ移します。配列とネストしたデータの検証、検証前後の処理、日本語メッセージ、そして形式チェックとビジネスルールの境界線を扱います。

出力の設計 — API Resource でレスポンスの形を決める

Laravel API 開発ガイドの第7章。モデルをそのまま返す危うさを示し、API Resource でレスポンスの形をコードとして固定します。リレーションの含め方、条件付きの項目、日付・金額・enum の表現、data ラッピングを扱います。

認可 — Gate と Policy で「誰が何をできるか」を決める

Laravel API 開発ガイドの第12章。注文の一覧と詳細を作りながら、Policy によるモデル単位の認可を実装します。一覧 API では Policy が効かない理由、403 と 404 の使い分け、認可漏れをテストで検出する方法を扱います。

認証 — Sanctum によるトークン認証

Laravel API 開発ガイドの第11章。Sanctum で会員登録・ログイン・ログアウトを作り、ルートをトークン認証で保護します。ゲスト注文を会員注文へ移す手順、トークンの権限と有効期限、テストでの認証済み状態の作り方まで扱います。