# top-think/thinkphp architecture

> Architecture diagram of the GitHub repository top-think/thinkphp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/top-think/thinkphp
- Repository: https://github.com/top-think/thinkphp

## Overview

##### Project type and purpose
- This is ThinkPHP v3.2, a lightweight, object-oriented PHP web-application framework.
- It implements the classic MVC pattern and provides ORM, templating, caching, REST/RPC, cloud-platform support, etc.

##### File structure
- Top-level directories:
- Application – where the user’s controllers, models, views (V layer) and module code live (currently empty).
- Public – web assets (CSS, JS, images), plus a README and placeholder index.html.
- ThinkPHP – the core framework code:
– Common (global helper functions)
– Conf (configuration files: debug, conventions)
– Lang (multi-language packs)
– Library (core MVC classes, cache, DB, session, template engines, upload drivers, log, verify, vendor)
– Mode (deployment modes: Lite, Api, SAE)
– Tpl (framework default view templates for errors, redirects)
– ThinkPHP.php (bootstrap entry for the framework library)
- Other files: index.php (front-controller entry point), .htaccess, composer.json

##### Extract architecture insights from the README
- MVC foundation with front-controller dispatch
- Plug-and-play drivers for DB, Cache, Session, Image, Upload, Crypt
- Support for RESTful controllers, CLI, RPC, NoSQL (MongoDB), cloud platforms (SAE, BAE)
- Security layers: XSS/SQL-injection protection, form validation, token verification

a. Main components:
- Client (Browser, Mobile app)
- Web Server (Apache/Nginx) + PHP interpreter
- Front Controller (index.php)
- ThinkPHP Core Modules:
– Router & Dispatcher
– Request/Response & Security filters
– Controller layer
– Model layer (ORM + Db drivers)
– View/Template Engine
– Auxiliary Services: Cache, Session, Logging, Upload, Verification
- Data Stores: SQL-DB, NoSQL, Cache stores (Redis/Memcached), File storage, Cloud services (SAE/BAE)
- Public folder (static assets CDN)
b. Relationships & data flows:
– Controller → View Engine → Response → Client
– Controller or Service → External RPC/REST → External services
– Lifecycle hooks or behaviors loaded by the core for security, logging, caching
c. Architectural patterns & design principles:
– MVC separation, single entry point (front-controller)
– Driver pattern for interchangeable cache/DB/storage adapters
– Behavior/Hook system for cross-cutting concerns
– Mode-based branching for different deployment targets (Api, Lite, SAE)
d. Key technologies, frameworks, libraries:
– PHP 5.x+ core
– Apache2 license, Composer for dependency management
– ThinkPHP core library components under ThinkPHP/Library
– Optional drivers: MySQL, PDO, MongoDB, Memcache, Redis, XCache, Qiniu/BCS uploads

##### Key points
- Every architectural layer: HTTP entry, core loader, routing, MVC layers, drivers, services.
- Extensibility points: where custom behaviors, hooks, or third-party libraries integrate.
- This will give a complete, accurate picture of how ThinkPHP 3.2 orchestrates request handling, business logic, and data persistence.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Application
    subgraph "User Application"
        AppMod["Application (Controllers, Models, Views)"]:::application
    end

    %% Entry & Bootstrap
    subgraph "Entry & Bootstrap"
        WebServer["Web Server (Apache/Nginx) + PHP"]:::external
        Index["index.php<br/>(Front Controller)"]:::core
        Bootstrap["ThinkPHP.php<br/>Framework Bootstrap"]:::core
        Functions["functions.php"]:::core
        Conf["Conf (Config Files)"]:::core
        Lang["Lang (i18n)"]:::core
        Mode["Mode (Lite/API/SAE)"]:::core
    end

    %% Core MVC Pipeline
    subgraph "Core MVC Pipeline"
        Router["Router & Dispatcher"]:::core
        Filters["Behaviors/Hooks (Request/Response & Security)"]:::core
        Controller["Controller Base"]:::core
        Model["Model & ORM"]:::core
        DbDriver["DB Drivers"]:::core
        ViewEngine["Template Engine"]:::core
        TemplateDrv["Template Drivers"]:::core
        TagLib["Template TagLib"]:::core
    end

    %% Auxiliary Services
    subgraph "Auxiliary Services"
        Cache["Cache"]:::service
        CacheDrv["Cache Drivers"]:::service
        Session["Session"]:::service
        SessionDrv["Session Drivers"]:::service
        Log["Logging"]:::service
        LogDrv["Log Drivers"]:::service
        Storage["Storage"]:::service
        StorageDrv["Storage Drivers"]:::service
        Upload["Upload"]:::service
        UploadDrv["Upload Drivers"]:::service
        Qiniu["Qiniu Driver"]:::service
        Bcs["BCS Driver"]:::service
        Image["Image"]:::service
        ImageDrv["Image Drivers"]:::service
        Crypt["Crypt"]:::service
        CryptDrv["Crypt Drivers"]:::service
        Verify["Verify"]:::service
        Bgs["BGs Driver"]:::service
        Ttfs["TTFS Driver"]:::service
        Zhttfs["ZHTTFS Driver"]:::service
        Vendor["Vendor Integrations"]:::service
    end

    %% Data Stores & External
    subgraph "Data Stores & External"
        SQLDB["SQL Database"]:::datastore
        NoSQL["NoSQL Database"]:::datastore
        CacheStore["Redis/Memcached"]:::datastore
        FileStore["File Storage"]:::datastore
        Cloud["Cloud Services (SAE/BAE)"]:::external
    end

    %% Public Assets
    subgraph "Public Assets"
        PublicAssets["Public/ (Static Assets)"]:::external
    end

    %% Connections
    Client["Client (Browser/Mobile)"]:::external
    Client --> WebServer
    WebServer --> Index
    Index --> Bootstrap
    Bootstrap --> Functions
    Bootstrap --> Conf
    Bootstrap --> Lang
    Bootstrap --> Mode
    Bootstrap --> Router
    Router --> Filters
    Filters --> Controller
    Controller --> Model
    Model --> DbDriver
    DbDriver --> SQLDB
    DbDriver --> NoSQL
    Controller --> ViewEngine
    ViewEngine --> TemplateDrv
    ViewEngine --> TagLib
    ViewEngine --> Client
    Controller --> Cache
    Controller --> Session
    Controller --> Log
    Controller --> Storage
    Controller --> Upload
    Controller --> Image
    Controller --> Crypt
    Controller --> Verify
    Controller --> Vendor

    Cache --> CacheDrv
    CacheDrv --> CacheStore
    Session --> SessionDrv
    SessionDrv --> CacheStore
    Log --> LogDrv
    LogDrv --> FileStore
    Storage --> StorageDrv
    StorageDrv --> FileStore
    StorageDrv --> Cloud
    Upload --> UploadDrv
    UploadDrv --> FileStore
    UploadDrv --> Cloud
    UploadDrv --> Qiniu
    UploadDrv --> Bcs
    Image --> ImageDrv
    ImageDrv --> FileStore
    Crypt --> CryptDrv
    Verify --> Bgs
    Verify --> Ttfs
    Verify --> Zhttfs

    PublicAssets --> Client

    AppMod --> Controller
    AppMod --> Model
    AppMod --> ViewEngine

    %% Click Events
    click AppMod "https://github.com/top-think/thinkphp/tree/master/Application/"
    click PublicAssets "https://github.com/top-think/thinkphp/tree/master/Public/"
    click Index "https://github.com/top-think/thinkphp/blob/master/index.php"
    click Bootstrap "https://github.com/top-think/thinkphp/blob/master/ThinkPHP/ThinkPHP.php"
    click Functions "https://github.com/top-think/thinkphp/blob/master/ThinkPHP/Common/functions.php"
    click Conf "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Conf/"
    click Lang "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Lang/"
    click Mode "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Mode/"
    click Router "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Controller/"
    click Filters "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Behavior/"
    click Controller "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Controller/"
    click Model "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Model/"
    click DbDriver "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Db/Driver/"
    click ViewEngine "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Template/"
    click TemplateDrv "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Template/Driver/"
    click TagLib "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Template/TagLib/"
    click Cache "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Cache/"
    click CacheDrv "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Cache/Driver/"
    click Session "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Session/"
    click SessionDrv "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Session/Driver/"
    click Log "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Log/"
    click LogDrv "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Log/Driver/"
    click Storage "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Storage/"
    click StorageDrv "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Storage/Driver/"
    click Upload "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Upload/"
    click UploadDrv "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Upload/Driver/"
    click Qiniu "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Upload/Driver/Qiniu/"
    click Bcs "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Upload/Driver/Bcs/"
    click Image "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Image/"
    click ImageDrv "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Image/Driver/"
    click Crypt "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Crypt/"
    click CryptDrv "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Crypt/Driver/"
    click Verify "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Verify/"
    click Bgs "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Verify/bgs/"
    click Ttfs "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Verify/ttfs/"
    click Zhttfs "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Think/Verify/zhttfs/"
    click Vendor "https://github.com/top-think/thinkphp/tree/master/ThinkPHP/Library/Vendor/"

    %% Styles
    classDef application fill:#f9f,stroke:#333,stroke-width:1px
    classDef core fill:#bbf,stroke:#333,stroke-width:1px
    classDef service fill:#bfb,stroke:#333,stroke-width:1px
    classDef datastore fill:#feb,stroke:#333,stroke-width:1px
    classDef external fill:#fdd,stroke:#333,stroke-width:1px
```
