# f1amy/laravel-realworld-example-app architecture

> Architecture diagram of the GitHub repository f1amy/laravel-realworld-example-app, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/f1amy/laravel-realworld-example-app
- Repository: https://github.com/f1amy/laravel-realworld-example-app

## Overview

##### Project Type and Purpose
- This is a “backend‐only” RESTful API server built on the Laravel framework.
- It implements the RealWorld API spec: CRUD on articles, comments, profiles, tags, user auth via JWT, pagination, etc.
- It’s meant to run in Docker/Sail, exposing `/api` endpoints to any frontend.

##### File Structure
- Top-level code sits in `app/` (Auth, Http, Jwt, Models, Policies, Console, Exceptions, Contracts, Providers).
- Routing lives in `routes/api.php`, `routes/web.php`.
- HTTP layer: public/index.php → controllers → requests/validators → resources/transformers → Eloquent models → database.
- Config in `config/` (db, cache, jwt, etc).
- Docker integration via `docker-compose.yml` and Laravel Sail scripts.
- Tests under `tests/` (Feature & Unit).

##### README Insights
- Confirms RESTful nature, JWT auth in `app/Jwt`, PostgreSQL backend via Sail.
- Mentions OpenAPI (Swagger) endpoint under `/api/documentation`.

a. Main Components
- External Client(s): Browser or mobile UI calling the API
- HTTP Server / Docker Proxy: Nginx (or Sail defaults) forwarding to PHP‐FPM
- Laravel Application (PHP-FPM container) subdivided into layers:
– Middleware (auth, cors, throttling)
– Controllers & Form Requests (validation)
– Resources (transformers)
– Services / JwtGuard / Custom JWT library
– Eloquent Models & Policies (domain logic)
- Database (PostgreSQL container)
- (Optional) Cache / Queue / Broadcast services (Redis, Horizon)
- Swagger UI (static view served from `storage/api-docs`)
- CI/CD pipelines (GitHub Actions for tests, static analysis)

b. Relationships & Interactions
1. Client → HTTP Server (80/443) → Laravel `public/index.php`.
2. Kernel dispatches through HTTP Middleware chain.
3. Route resolves to Controller method (e.g., `ArticleController@index`).
4. Controller uses FormRequest to validate, then calls Eloquent Model &/or JwtGuard.
5. Models read/write to PostgreSQL.
6. Responses are transformed via API Resources and returned as JSON.
7. CI/CD workflows monitor GitHub repo → trigger tests, coverage, static analysis.

c. Architectural Patterns & Principles
- MVC + layered architecture
- Dependency inversion via Contracts (interfaces in `app/Contracts`)
- Single Responsibility: FormRequest, Resources, Controllers, Models, Policies
- JWT‐based stateless authentication (custom implementation in `app/Jwt`)
- Configuration over hard-coding (`config/*.php`)
- Dockerized deployments (infrastructure as code)

d. Key Technologies & Frameworks
- PHP 8.x, Laravel 10.x
- Eloquent ORM
- Custom JWT library (builder, parser, validator)
- PostgreSQL (via Sail)
- GitHub Actions for CI
- PHPUnit & Laravel’s test suite
- Swagger/OpenAPI

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Client
    Client["External Client (Browser/Mobile)"]:::external

    %% HTTP Layer
    subgraph "HTTP Layer"
        Nginx["Nginx / Sail Proxy"]:::infra
        PHPFPM["PHP-FPM<br/>(public/index.php)"]:::infra
    end

    Client -->|"HTTP Request"| Nginx
    Nginx -->|"Forward"| PHPFPM

    click Nginx "https://github.com/f1amy/laravel-realworld-example-app/blob/main/docker-compose.yml"
    click PHPFPM "https://github.com/f1amy/laravel-realworld-example-app/blob/main/public/index.php"

    %% Laravel Application
    subgraph "Laravel Application" 
        direction TB

        subgraph "Routing"
            API_Routes["routes/api.php"]:::infra
            Web_Routes["routes/web.php"]:::infra
        end

        subgraph "HTTP Kernel & Middleware"
            Kernel["Kernel<br/>(app/Http/Kernel.php)"]:::app
            Middleware["Middleware<br/>(app/Http/Middleware/)"]:::app
        end

        subgraph "Controllers & Form Requests"
            ArticlesCtrl["ArticleController"]:::app
            CommentsCtrl["CommentsController"]:::app
            FavoritesCtrl["FavoritesController"]:::app
            AuthCtrl["AuthController"]:::app
            ProfileCtrl["ProfileController"]:::app
            TagsCtrl["TagsController"]:::app
            UserCtrl["UserController"]:::app
            FormRequests["FormRequests<br/>(app/Http/Requests/Api/)"]:::app
        end

        subgraph "Resources / Transformers"
            ArticleRes["ArticleResource"]:::app
            ArticlesCol["ArticlesCollection"]:::app
            CommentRes["CommentResource"]:::app
            CommentsCol["CommentsCollection"]:::app
            TagRes["TagResource"]:::app
            TagsCol["TagsCollection"]:::app
            ProfileRes["ProfileResource"]:::app
            UserRes["UserResource"]:::app
        end

        subgraph "JWT Services & Guard"
            JwtGuard["JwtGuard"]:::app
            JwtBuilder["Builder / Parser / Validator<br/>(app/Jwt/)"]:::app
            JwtContracts["Jwt Interfaces<br/>(app/Contracts/)"]:::app
        end

        subgraph "Eloquent Models & Policies"
            Models["Models<br/>(app/Models/)"]:::data
            Policies["Policies<br/>(app/Policies/)"]:::data
        end

        subgraph "Configuration"
            Config["config/*.php"]:::infra
        end
    end

    %% Database
    subgraph "Database"
        PostgreSQL["PostgreSQL<br/>(Container)"]:::data
    end

    %% CI/CD & Tests
    subgraph "DevOps & Testing"
        GitHubActions["GitHub Actions<br/>(.github/workflows/)"]:::infra
        Tests["Tests<br/>(tests/)"]:::infra
        SwaggerUI["Swagger UI<br/>(storage/api-docs/)"]:::infra
    end

    %% Flows within Laravel App
    PHPFPM --> Kernel
    Kernel --> Middleware
    Kernel --> API_Routes
    Kernel --> Web_Routes

    API_Routes --> ArticlesCtrl
    API_Routes --> CommentsCtrl
    API_Routes --> FavoritesCtrl
    API_Routes --> AuthCtrl
    API_Routes --> ProfileCtrl
    API_Routes --> TagsCtrl
    API_Routes --> UserCtrl

    ArticlesCtrl --> FormRequests
    CommentsCtrl --> FormRequests
    AuthCtrl --> FormRequests
    ProfileCtrl --> FormRequests
    UserCtrl --> FormRequests

    ControllersAndForms --> ArticleRes
    ControllersAndForms --> CommentRes
    ControllersAndForms --> TagRes
    ControllersAndForms --> ProfileRes
    ControllersAndForms --> UserRes

    ArticlesCtrl --> JwtGuard
    AuthCtrl --> JwtGuard
    JwtGuard --> JwtBuilder
    JwtBuilder --> JwtContracts

    ArticlesCtrl --> Models
    CommentsCtrl --> Models
    FavoritesCtrl --> Models
    AuthCtrl --> Models
    ProfileCtrl --> Models
    TagsCtrl --> Models
    UserCtrl --> Models

    Models --> Policies
    Models --> PostgreSQL
    PostgreSQL -->|Reads/Writes| Models

    PHPFPM --> Config
    PHPFPM --> SwaggerUI
    SwaggerUI -->|Serves Docs| Client

    GitHubActions --> Tests
    GitHubActions -->|CI/CD| PHPFPM

    %% Click Events
    click API_Routes "https://github.com/f1amy/laravel-realworld-example-app/blob/main/routes/api.php"
    click Web_Routes "https://github.com/f1amy/laravel-realworld-example-app/blob/main/routes/web.php"
    click Kernel "https://github.com/f1amy/laravel-realworld-example-app/blob/main/app/Http/Kernel.php"
    click Middleware "https://github.com/f1amy/laravel-realworld-example-app/tree/main/app/Http/Middleware/"
    click ArticlesCtrl "https://github.com/f1amy/laravel-realworld-example-app/blob/main/app/Http/Controllers/Api/Articles/ArticleController.php"
    click CommentsCtrl "https://github.com/f1amy/laravel-realworld-example-app/blob/main/app/Http/Controllers/Api/Articles/CommentsController.php"
    click FavoritesCtrl "https://github.com/f1amy/laravel-realworld-example-app/blob/main/app/Http/Controllers/Api/Articles/FavoritesController.php"
    click AuthCtrl "https://github.com/f1amy/laravel-realworld-example-app/blob/main/app/Http/Controllers/Api/AuthController.php"
    click ProfileCtrl "https://github.com/f1amy/laravel-realworld-example-app/blob/main/app/Http/Controllers/Api/ProfileController.php"
    click TagsCtrl "https://github.com/f1amy/laravel-realworld-example-app/blob/main/app/Http/Controllers/Api/TagsController.php"
    click UserCtrl "https://github.com/f1amy/laravel-realworld-example-app/blob/main/app/Http/Controllers/Api/UserController.php"
    click FormRequests "https://github.com/f1amy/laravel-realworld-example-app/tree/main/app/Http/Requests/Api/"
    click ArticleRes "https://github.com/f1amy/laravel-realworld-example-app/blob/main/app/Http/Resources/Api/ArticleResource.php"
    click ArticlesCol "https://github.com/f1amy/laravel-realworld-example-app/blob/main/app/Http/Resources/Api/ArticlesCollection.php"
    click CommentRes "https://github.com/f1amy/laravel-realworld-example-app/blob/main/app/Http/Resources/Api/CommentResource.php"
    click CommentsCol "https://github.com/f1amy/laravel-realworld-example-app/blob/main/app/Http/Resources/Api/CommentsCollection.php"
    click TagRes "https://github.com/f1amy/laravel-realworld-example-app/blob/main/app/Http/Resources/Api/TagResource.php"
    click TagsCol "https://github.com/f1amy/laravel-realworld-example-app/blob/main/app/Http/Resources/Api/TagsCollection.php"
    click ProfileRes "https://github.com/f1amy/laravel-realworld-example-app/blob/main/app/Http/Resources/Api/ProfileResource.php"
    click UserRes "https://github.com/f1amy/laravel-realworld-example-app/blob/main/app/Http/Resources/Api/UserResource.php"
    click JwtGuard "https://github.com/f1amy/laravel-realworld-example-app/blob/main/app/Auth/JwtGuard.php"
    click JwtBuilder "https://github.com/f1amy/laravel-realworld-example-app/tree/main/app/Jwt/"
    click JwtContracts "https://github.com/f1amy/laravel-realworld-example-app/tree/main/app/Contracts/"
    click Models "https://github.com/f1amy/laravel-realworld-example-app/tree/main/app/Models/"
    click Policies "https://github.com/f1amy/laravel-realworld-example-app/tree/main/app/Policies/"
    click Config "https://github.com/f1amy/laravel-realworld-example-app/tree/main/config/"
    click PostgreSQL "https://github.com/f1amy/laravel-realworld-example-app/tree/main/database/migrations/"
    click GitHubActions "https://github.com/f1amy/laravel-realworld-example-app/tree/main/.github/workflows/"
    click Tests "https://github.com/f1amy/laravel-realworld-example-app/tree/main/tests/"

    %% Styles
    classDef external fill:#cccccc,stroke:#333,stroke-width:1px
    classDef infra fill:#999999,stroke:#333,stroke-width:1px
    classDef app fill:#88cc88,stroke:#333,stroke-width:1px
    classDef data fill:#ffcc66,stroke:#333,stroke-width:1px
```
