# daveh/php-rest-api architecture

> Architecture diagram of the GitHub repository daveh/php-rest-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/daveh/php-rest-api
- Repository: https://github.com/daveh/php-rest-api

## Overview

This is a small PHP REST API for managing product records. A client requests the `/products` collection or an individual product; `index.php` routes the request to `ProductController`, which validates writes and handles list, read, create, update, and delete operations through `ProductGateway`. The gateway uses `Database` to connect to MySQL. The controller encodes JSON responses and sets relevant HTTP statuses. Error and exception handlers are registered at the entry point. README capabilities are reflected where supported by the sampled source; database schema details were not inspected.

Main entry points: [`src/ProductController.php`](https://github.com/daveh/php-rest-api/blob/main/src/ProductController.php) (Product controller), [`src/ProductGateway.php`](https://github.com/daveh/php-rest-api/blob/main/src/ProductGateway.php) (Product gateway), [`index.php`](https://github.com/daveh/php-rest-api/blob/main/index.php) (HTTP entry point), [`src/Database.php`](https://github.com/daveh/php-rest-api/blob/main/src/Database.php) (Database connector)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API handling"]
  node_entry["HTTP entry point<br/>[index.php]"]
  node_errors["Error handlers<br/>[ErrorHandler.php]"]
end

subgraph group_products["Product operations"]
  node_controller["Product controller"]
end

subgraph group_persistence["Persistence and errors"]
  node_gateway["Product gateway<br/>[ProductGateway.php]"]
  node_database["Database connector<br/>[Database.php]"]
end

node_client(("API client"))
node_mysql[("MySQL database")]

node_client -->|"sends HTTP request"| node_entry
node_entry -->|"dispatches request"| node_controller
node_entry -->|"registers handlers"| node_errors
node_entry -->|"creates connector"| node_database
node_entry -->|"creates gateway"| node_gateway
node_entry -->|"creates controller"| node_controller
node_controller -->|"reads product"| node_gateway
node_controller -->|"lists products"| node_gateway
node_controller -->|"creates product"| node_gateway
node_controller -->|"updates product"| node_gateway
node_controller -->|"deletes product"| node_gateway
node_gateway -->|"returns records"| node_controller
node_gateway -->|"requests connection"| node_database
node_database -->|"opens PDO connection"| node_mysql
node_gateway -->|"queries product data"| node_mysql
node_controller -->|"returns JSON response"| node_client

click node_entry "https://github.com/daveh/php-rest-api/blob/main/index.php"
click node_controller "https://github.com/daveh/php-rest-api/blob/main/src/ProductController.php"
click node_gateway "https://github.com/daveh/php-rest-api/blob/main/src/ProductGateway.php"
click node_database "https://github.com/daveh/php-rest-api/blob/main/src/Database.php"
click node_errors "https://github.com/daveh/php-rest-api/blob/main/src/ErrorHandler.php"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry,node_errors,node_client toneBlue
class node_controller,node_mysql toneAmber
class node_gateway,node_database toneMint
```

## Components

### API handling

- [HTTP entry point](https://github.com/daveh/php-rest-api/blob/main/index.php): `index.php`
- [Error handlers](https://github.com/daveh/php-rest-api/blob/main/src/ErrorHandler.php): `src/ErrorHandler.php`

### Product operations

- [Product controller](https://github.com/daveh/php-rest-api/blob/main/src/ProductController.php): `src/ProductController.php`

### Persistence and errors

- [Product gateway](https://github.com/daveh/php-rest-api/blob/main/src/ProductGateway.php): `src/ProductGateway.php`
- [Database connector](https://github.com/daveh/php-rest-api/blob/main/src/Database.php): `src/Database.php`

### Other components

- **API client**
- **MySQL database**

## Connections

- API client → HTTP entry point: sends HTTP request
- HTTP entry point → Product controller: dispatches request
- HTTP entry point → Error handlers: registers handlers
- HTTP entry point → Database connector: creates connector
- HTTP entry point → Product gateway: creates gateway
- HTTP entry point → Product controller: creates controller
- Product controller → Product gateway: reads product
- Product controller → Product gateway: lists products
- Product controller → Product gateway: creates product
- Product controller → Product gateway: updates product
- Product controller → Product gateway: deletes product
- Product gateway → Product controller: returns records
- Product gateway → Database connector: requests connection
- Database connector → MySQL database: opens PDO connection
- Product gateway → MySQL database: queries product data
- Product controller → API client: returns JSON response
