# yiisoft/yii architecture

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

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

## Overview

##### Project type and purpose
- This is an open-source, full-stack PHP Web framework (Yii 1.1), component-based and MVC-driven, shipping with:
- framework core (library code)
- command-line tools (yiic, yiit, yiilite, build scripts)
- sample Web apps in demos/
- documentation viewer and static guides in docs/
- requirement checker in requirements/
- test suite in tests/

##### File-structure analysis
- Top-level directories:
- framework/ – core application components, sub-packages (base, web, console, db, caching, i18n, logging, utils, validators, zii, gii, vendors)
- demos/ – example MVC Web apps using the framework
- docs/ – interactive API viewer (docs/viewer) + multi-language guides
- requirements/ – system-preflight checker
- build/ – Ant/CLI scripts for packaging, API extraction, LaTeX/Markdown conversion
- tests/ – PHPUnit test harness initializing TestApplication and unit/integration tests

##### Key insights from README
- Framework entry point for Web: yii.php via index.php
- Framework entry point for CLI: yiic (commands under framework/cli/)
- PHP ≥5.1, MVC, front-controller, component registry, event system

##### Main components
a. Web‐request flow
1. Browser → Web server (Apache/Nginx)
2. index.php → yii.php → CWebApplication
3. CWebApplication initializes:
– CModule/CComponent registry
– CUrlManager dispatch
– CController → CAction
4. Controller interacts with Models (CActiveRecord via CDbConnection, query builder)
5. Models ↔ Database servers (MySQL, SQLite, PostgreSQL, Oracle, SQL Server)
6. Controller renders Views → HTML/JSON/XML → Browser
7. Optional layers: caching (APC, MemCache, Redis, FileCache), logging, i18n, widgets (jQuery, UI assets)

b. CLI‐tool flow
1. Terminal → yiic → CConsoleApplication
2. yiic loads commands under framework/cli/commands/ (e.g., CrudCommand, ShellCommand)
3. Commands generate skeleton code, migrations, messages, etc.

c. Documentation viewer
1. Browser → docs/viewer/index.php → GuideController/BlogController
2. Loads static .txt files under docs/guide and docs/blog
3. Uses MarkdownParser and APIModel to render docs

d. Build system
– build/build.xml (Ant) invokes commands under build/commands/ to produce API HTML, CHM, LaTeX outputs

e. Test suite
– tests/bootstrap.php instantiates TestApplication (a CWebApplication subclass)
– PHPUnit runs tests across framework sub-packages

##### Relationships and interactions
- All entry points depend on the core framework packages (base, web, console, db, caching, i18n, logging, utils, validators, zii, gii)
- Sample apps (demos/) reference framework/ and vendors/
- docs/viewer references framework/utils/CMarkdownParser and APIModel

##### Architectural patterns & principles
- Front Controller (index.php → CWebApplication)
- MVC (Controller, Model = ActiveRecord, View templates)
- Component registry & event system (CComponent, behaviors, events)
- Extensible modules (Gii code generator, Zii widget library, third-party vendors)
- Separation of concerns: core vs. application vs. tools vs. docs/tests

##### Technologies, frameworks, libraries
- PHP (≥5.1, up to 8.3)
- CLI via yiic/Ant/build scripts
- HTML/CSS/JS assets (jQuery, jQuery UI, TextHighlighter, HTMLPurifier)
- Databases: PDO adapters for MySQL, SQLite, PostgreSQL, Oracle, SQL Server
- Caching backends: APC, MemCache, Redis, FileCache
- Internationalization: CLDR data, Gettext, PHP message sources
- Unit testing: PHPUnit

##### Essential elements
- Framework core clusters
- Entry points
- Sample apps
- Tools & documentation
- External services
- Clear text labels on all relationships and components

## Diagram (Mermaid)

```mermaid
flowchart TB
  %% Entry Points
  subgraph "Entry Points" 
    classDef entry fill:#FFEEAA,stroke:#BBBB00
    Browser["Browser"]:::entry 
    CLI["Terminal"]:::entry
    BuildScript["Ant / CLI"]:::entry
    DocsBrowser["Browser"]:::entry
    ReqChecker["Browser/CLI"]:::entry
  end

  %% Entry Point Scripts
  Browser -->|"HTTP"| index_php["index.php"] 
  index_php -->|"includes"| yii_php["yii.php"]
  yii_php -->|"instantiates"| CWebApp["CWebApplication"]
  click index_php "https://github.com/yiisoft/yii/blob/master/index.php"
  click yii_php "https://github.com/yiisoft/yii/blob/master/framework/yii.php"
  click CWebApp "https://github.com/yiisoft/yii/blob/master/framework/web/CWebApplication.php"

  CLI -->|"CLI"| yiic_php["yiic.php"]
  yiic_php -->|"instantiates"| CConsoleApp["CConsoleApplication"]
  click yiic_php "https://github.com/yiisoft/yii/blob/master/framework/yiic.php"

  BuildScript -->|"runs"| BuildXML["build/build.xml"]
  click BuildXML "https://github.com/yiisoft/yii/blob/master/build/build.xml"

  DocsBrowser -->|"HTTP"| DocIndex["docs/viewer/index.php"]
  click DocIndex "https://github.com/yiisoft/yii/blob/master/docs/viewer/index.php"

  ReqChecker -->|"runs"| ReqIndex["requirements/index.php"]
  click ReqIndex "https://github.com/yiisoft/yii/blob/master/requirements/index.php"

  %% Framework Core
  subgraph "Framework Core" 
    classDef core fill:#DDEEFF,stroke:#0044BB
    framework["framework/"]:::core
  end

  CWebApp --> CModuleReg["CModule/CComponent Registry"]:::core
  CWebApp --> UrlManager["CUrlManager Dispatch"]:::core
  UrlManager --> Router["Router.dispatch()"]:::core
  Router --> Controller["CController"]:::core
  Controller --> Action["CAction"]:::core
  Controller --> Model["CActiveRecord"]:::core
  Model --> DBConn["CDbConnection"]:::core
  DBConn -->|"PDO"| DBServers["DB Servers"]:::external
  Controller --> View["View Renderer"]:::core
  View --> Browser

  %% Optional Services
  subgraph "Services" 
    classDef external fill:#FFDDDD,stroke:#BB0000
    DBServers
    Cache["Caching (APC,MemCache,Redis,File)"]:::external
    Logging["Logging"]:::external
    I18n["Internationalization"]:::external
  end
  CModuleReg --> Cache
  CModuleReg --> Logging
  CModuleReg --> I18n

  %% CLI Commands
  subgraph "CLI Commands" 
    classDef tools fill:#EEFFDD,stroke:#00BB44
    yiic_php --> CLICommands["framework/cli/commands/"]:::tools
  end
  click CLICommands "https://github.com/yiisoft/yii/tree/master/framework/cli/commands/"

  %% Documentation Viewer Components
  subgraph "Documentation Viewer" 
    classDef docs fill:#F0E0FF,stroke:#8000BB
    DocIndex --> GuideCtrl["GuideController"]:::docs
    DocIndex --> BlogCtrl["BlogController"]:::docs
    GuideCtrl --> Markdown["MarkdownParser"]:::docs
    BlogCtrl --> Markdown
    GuideCtrl --> APIModel["APIModel"]:::docs
    BlogCtrl --> APIModel
  end
  click GuideCtrl "https://github.com/yiisoft/yii/blob/master/docs/viewer/controllers/GuideController.php"
  click BlogCtrl "https://github.com/yiisoft/yii/blob/master/docs/viewer/controllers/BlogController.php"
  click Markdown "https://github.com/yiisoft/yii/blob/master/docs/viewer/components/MarkdownParser.php"
  click DocsConfig "https://github.com/yiisoft/yii/blob/master/docs/viewer/config/main.php"
  DocIndex --> DocsConfig
  click DocsConfig "https://github.com/yiisoft/yii/blob/master/docs/viewer/config/main.php"

  %% Requirements Checker
  subgraph "Requirements Checker" 
    classDef tools fill:#EEFFDD,stroke:#00BB44
    ReqIndex --> ReqViews["requirements/views/"]:::tools
    ReqIndex --> ReqMessages["requirements/messages/"]:::tools
  end
  click ReqViews "https://github.com/yiisoft/yii/tree/master/requirements/views/"
  click ReqMessages "https://github.com/yiisoft/yii/tree/master/requirements/messages/"

  %% Build System Commands
  subgraph "Build Commands" 
    classDef tools fill:#EEFFDD,stroke:#00BB44
    BuildXML --> BuildCmds["build/commands/"]:::tools
    BuildCmds --> APIBuild["build/commands/api/"]:::tools
  end
  click BuildCmds "https://github.com/yiisoft/yii/tree/master/build/commands/"
  click APIBuild "https://github.com/yiisoft/yii/tree/master/build/commands/api/"

  %% Sample Applications
  subgraph "Sample Web Apps" 
    classDef apps fill:#DDFFEE,stroke:#008844
    demos["demos/"]:::apps
    demos --> BlogDemo["demos/blog/"]:::apps
    demos --> Hangman["demos/hangman/"]:::apps
    demos --> Hello["demos/helloworld/"]:::apps
    demos --> Phonebook["demos/phonebook/"]:::apps
    BlogDemo --> framework
    Hangman --> framework
    Hello --> framework
    Phonebook --> framework
  end
  click BlogDemo "https://github.com/yiisoft/yii/tree/master/demos/blog/"
  click Hangman "https://github.com/yiisoft/yii/tree/master/demos/hangman/"
  click Hello "https://github.com/yiisoft/yii/tree/master/demos/helloworld/"
  click Phonebook "https://github.com/yiisoft/yii/tree/master/demos/phonebook/"

  %% Test Suite
  subgraph "Test Suite" 
    classDef tests fill:#FFEEDD,stroke:#BB5500
    Boot["tests/bootstrap.php"]:::tests
    TestApp["tests/TestApplication.php"]:::tests
    PHPUnitConfig["tests/phpunit.xml"]:::tests
    TestFramework["tests/framework/"]:::tests
    Boot --> TestApp
    Boot --> TestFramework
  end
  click Boot "https://github.com/yiisoft/yii/blob/master/tests/bootstrap.php"
  click TestApp "https://github.com/yiisoft/yii/blob/master/tests/TestApplication.php"
  click PHPUnitConfig "https://github.com/yiisoft/yii/blob/master/tests/phpunit.xml"
  click TestFramework "https://github.com/yiisoft/yii/tree/master/tests/framework/"

  %% Code Generator & Widgets
  subgraph "Gii & Zii Modules" 
    classDef core fill:#DDEEFF,stroke:#0044BB
    Gii["framework/gii/"]:::core
    Gii --> GiiGen["framework/gii/generators/"]:::core
    ZiiWidgets["framework/zii/widgets/"]:::core
    ZiiBehaviors["framework/zii/behaviors/"]:::core
  end
  click Gii "https://github.com/yiisoft/yii/tree/master/framework/gii/"
  click GiiGen "https://github.com/yiisoft/yii/tree/master/framework/gii/generators/"
  click ZiiWidgets "https://github.com/yiisoft/yii/tree/master/framework/zii/widgets/"
  click ZiiBehaviors "https://github.com/yiisoft/yii/tree/master/framework/zii/behaviors/"

  %% Legend
  subgraph "Legend"
    EntryEntry["Entry Points"]:::entry
    CoreCore["Framework Core"]:::core
    ExtExt["External Services"]:::external
    AppsApps["Sample Apps"]:::apps
    ToolsTools["Tools & Docs"]:::tools
    TestsTests["Test Suite"]:::tests
    DocsDocs["Documentation"]:::docs
  end

  class EntryEntry,CoreCore,ExtExt,AppsApps,ToolsTools,TestsTests,DocsDocs titleDef
```
