# yiisoft/yii2 architecture

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

- Interactive diagram: https://gitdiagram.com/yiisoft/yii2
- Repository: https://github.com/yiisoft/yii2

## Overview

##### Main Components

a. Core Framework Layer:
- Base Components (framework/base/)
- Database Layer (framework/db/)
- Caching System (framework/caching/)
- Security Components (framework/rbac/)
- Web Components (framework/web/)
- Console Components (framework/console/)

b. Service Layer:
- Dependency Injection Container (framework/di/)
- Service Locator
- Event Handling System

c. MVC Components:
- Models (Active Record implementation)
- Views & Widgets
- Controllers
- URL Management

**[Client Layer]**
- ↑↓
**[Web/Console Entry Points]**
- ↑↓
**[Application Layer]**
- ↑↓
**[Core Framework Components]**
- ↑↓
**[Service Layer]**
- ↑↓
**[Database/Storage Layer]**

##### Key Relationships

a. Request Flow:
- HTTP Request → URL Manager → Controller → Action → View
- Console Request → Command → Action

b. Data Flow:
- Models ↔ Database
- Controllers ↔ Models
- Views ↔ Controllers

c. Service Interactions:
- Dependency Injection connections
- Event handling paths
- Caching system integration

##### Important Subsystems

- RBAC (Role-Based Access Control)
- Caching architecture
- Database abstraction layer
- Asset management system

- Client Layer   |

- Application    |
- Infrastructure |

- Core Services  |

- Data Access    |

Remember to:
- Keep the abstraction level consistent
- Architectural relationships
- Version compatibility notes
- Document integration points
- Configuration flows

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        Browser["Web Browser"]
        Console["Console Client"]
    end

    subgraph "Entry Points"
        WebEntry["Web Entry Point"]:::web
        ConsoleEntry["Console Entry"]:::console
        click ConsoleEntry "https://github.com/yiisoft/yii2/blob/master/framework/console/Application.php"
    end

    subgraph "Application Layer"
        URLManager["URL Manager"]:::web
        Request["Request Handler"]:::web
        Response["Response Handler"]:::web
        User["User Component"]:::web
        
        click URLManager "https://github.com/yiisoft/yii2/blob/master/framework/web/UrlManager.php"
        click Request "https://github.com/yiisoft/yii2/blob/master/framework/web/Request.php"
        click Response "https://github.com/yiisoft/yii2/blob/master/framework/web/Response.php"
        click User "https://github.com/yiisoft/yii2/blob/master/framework/web/User.php"
    end

    subgraph "MVC Components"
        Controller["Base Controller"]:::mvc
        View["View System"]:::mvc
        Widget["Widgets"]:::mvc
        AssetBundle["Asset Bundle"]:::mvc
        AssetManager["Asset Manager"]:::mvc
        
        click Controller "https://github.com/yiisoft/yii2/blob/master/framework/web/Controller.php"
        click View "https://github.com/yiisoft/yii2/blob/master/framework/web/View.php"
        click Widget "https://github.com/yiisoft/yii2/tree/master/framework/widgets/"
        click AssetBundle "https://github.com/yiisoft/yii2/blob/master/framework/web/AssetBundle.php"
        click AssetManager "https://github.com/yiisoft/yii2/blob/master/framework/web/AssetManager.php"
    end

    subgraph "Core Framework"
        BaseObject["Base Object"]:::core
        Component["Component System"]:::core
        Event["Event System"]:::core
        
        click BaseObject "https://github.com/yiisoft/yii2/blob/master/framework/base/BaseObject.php"
        click Component "https://github.com/yiisoft/yii2/blob/master/framework/base/Component.php"
        click Event "https://github.com/yiisoft/yii2/blob/master/framework/base/Event.php"
    end

    subgraph "Service Layer"
        DI["Dependency Injection"]:::service
        ServiceLocator["Service Locator"]:::service
        
        click DI "https://github.com/yiisoft/yii2/blob/master/framework/di/Container.php"
        click ServiceLocator "https://github.com/yiisoft/yii2/blob/master/framework/di/ServiceLocator.php"
    end

    subgraph "Security Layer"
        RBAC["RBAC Manager"]:::security
        Permission["Permissions"]:::security
        Role["Roles"]:::security
        
        click RBAC "https://github.com/yiisoft/yii2/blob/master/framework/rbac/BaseManager.php"
        click Permission "https://github.com/yiisoft/yii2/blob/master/framework/rbac/Permission.php"
        click Role "https://github.com/yiisoft/yii2/blob/master/framework/rbac/Role.php"
    end

    subgraph "Data Layer"
        ActiveRecord["Active Record"]:::data
        QueryBuilder["Query Builder"]:::data
        DBConnection["Database Connection"]:::data
        Cache["Cache Interface"]:::data
        FileCache["File Cache"]:::data
        MemCache["Memory Cache"]:::data
        
        click ActiveRecord "https://github.com/yiisoft/yii2/blob/master/framework/db/ActiveRecord.php"
        click QueryBuilder "https://github.com/yiisoft/yii2/blob/master/framework/db/QueryBuilder.php"
        click DBConnection "https://github.com/yiisoft/yii2/blob/master/framework/db/Connection.php"
        click Cache "https://github.com/yiisoft/yii2/blob/master/framework/caching/CacheInterface.php"
        click FileCache "https://github.com/yiisoft/yii2/blob/master/framework/caching/FileCache.php"
        click MemCache "https://github.com/yiisoft/yii2/blob/master/framework/caching/MemCache.php"
    end

    %% Connections
    Browser --> WebEntry
    Console --> ConsoleEntry
    WebEntry --> URLManager
    URLManager --> Request
    Request --> Controller
    Controller --> View
    View --> Response
    Controller --> ActiveRecord
    ActiveRecord --> DBConnection
    Controller --> Cache
    Cache --> FileCache
    Cache --> MemCache
    Controller --> RBAC
    RBAC --> Role
    RBAC --> Permission
    Controller --> DI
    DI --> ServiceLocator
    View --> Widget
    View --> AssetBundle
    AssetBundle --> AssetManager

    %% Styles
    classDef core fill:#2196F3,color:white
    classDef web fill:#4CAF50,color:white
    classDef data fill:#FFC107,color:black
    classDef security fill:#FF5722,color:white
    classDef service fill:#9C27B0,color:white
    classDef mvc fill:#607D8B,color:white
    classDef console fill:#795548,color:white

    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["Web Components"]:::web
        L3["Data Components"]:::data
        L4["Security Components"]:::security
        L5["Service Components"]:::service
        L6["MVC Components"]:::mvc
        L7["Console Components"]:::console
    end
```
