# andrejrs/php-mvc-example architecture

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

- Interactive diagram: https://gitdiagram.com/andrejrs/php-mvc-example
- Repository: https://github.com/andrejrs/php-mvc-example

## Overview

##### Identifying the Main Components
- Front Controller/Entry Point: Located under public/index.php. This is where all incoming HTTP requests are directed.
- Core Classes (in the src directory): These include key abstractions like Controller, Model, Request, Router, Validator, View, and others. They encapsulate the core functionality of the MVC framework.
- Application Layer (in the App directory):
– Controllers (e.g., App/Controllers/Home.php): Handle incoming requests.
– Models (e.g., Customer, Order, OrderItem, Statistic in App/Models): Represent the business data and interaction with the database.
– Views (e.g., the page templates in App/Views/Page and App/Views/Template.php): Manage how information is presented.
– Routing / Namespacing (App/Routes.php and App/Namespaces.php): Define the routes and namespace mapping used by the framework.
- Configuration (in the Config directory): Contains files like Application.php and Database.php that set up configuration parameters for the application and database.
- Database & Seeder: The database directory holds SQL dumps and seed classes (e.g., OrderSeeder.php) to initialize test data.
- Public Assets: Located in the public directory, including assets like CSS, JavaScript (dashboard.js), and .htaccess for server configurations.

##### Relationships and Interactions
- The Request (src/Request.php) then processes input data, which is passed to the Router (src/Router.php) to determine the appropriate controller from App/Controllers.
- Controllers leverage Models (from App/Models) to interact with the database, guided by configuration parameters from the Config directory.
- After processing, controllers hand off data to Views (in App/Views) to generate user output.
- The Seeder (from seeder or database/seed files) provides a one-off use case to populate the database.
- The configuration and core components ensure that the MVC layers remain loosely coupled.

##### Architectural Patterns and Design Principles
- MVC Pattern: The separation of concerns is explicit—Controllers process requests, Models handle database logic, and Views take care of presentation.
- Front Controller: All traffic passes through a single entry point (public/index.php) before being directed to the proper route.
- Autoloading and Namespacing: The src/Psr4AutoloaderClass.php, together with App/Namespaces.php, demonstrates adherence to PSR-4 standards.
- Dependency Centralization: Configuration and database connection settings are centrally maintained in the Config directory.

##### Including Technologies and Frameworks
- PHP (tested on PHP 7.1.14).
- MySQL for database operations.
- Web servers: Apache or Nginx.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Frontend Entry Point
    FC["Front Controller"]:::frontend

    %% Core Components
    subgraph "Core Components"
        AL["Autoloader"]:::core
        REQ["Request"]:::core
        ROT["Router"]:::core
        VAL["Validator"]:::core
        ERR["Error Handler"]:::core
        CONT["Base Controller"]:::core
        MOD["Base Model"]:::core
        VW["Base View"]:::core
    end

    %% Routing & Namespacing
    subgraph "Routing & Namespacing"
        ROUTE["Routes Definition"]:::routing
        NSPACE["Namespace Configuration"]:::routing
    end

    %% Application Layer
    subgraph "Application Layer"
        %% Controllers
        AC["Home Controller"]:::business
        %% Models
        CUST["Customer Model"]:::business
        ORD["Order Model"]:::business
        ORD_ITM["OrderItem Model"]:::business
        STAT["Statistic Model"]:::business
        %% Views
        PAGE["Page Template"]:::frontend
        GEN["General Template"]:::frontend
        ERRVIEW["Error View"]:::frontend
    end

    %% Configuration
    subgraph "Configuration"
        APP_CONF["Application Config"]:::config
        DB_CONF["Database Config"]:::config
    end

    %% Database & Seeder
    subgraph "Database"
        SEED["Seeder"]:::database
        SQL1["SQL Dump (2018-02-24)"]:::database
        SQL2["SQL Dump (2018-03-01)"]:::database
    end

    %% Public Assets
    subgraph "Public Assets"
        HTACCESS[".htaccess"]:::assets
        CSS["CSS Assets"]:::assets
        DASH["JavaScript Assets"]:::assets
    end

    %% Flow Connections
    FC -->|"initializes"| AL
    FC -->|"handles request"| REQ
    REQ -->|"routes request"| ROT
    ROT -->|"uses"| ROUTE
    ROT -->|"uses"| NSPACE
    ROT -->|"selects controller"| AC
    AC -->|"calls business logic"| CUST
    AC -->|"calls business logic"| ORD
    AC -->|"calls business logic"| ORD_ITM
    AC -->|"calls business logic"| STAT
    AC -->|"renders view"| PAGE
    AC -->|"renders view"| GEN
    AC -->|"error handled by"| ERRVIEW

    %% Config usage
    AC -->|"reads config"| APP_CONF
    CUST -->|"db connection"| DB_CONF
    ORD -->|"db connection"| DB_CONF
    ORD_ITM -->|"db connection"| DB_CONF
    STAT -->|"db connection"| DB_CONF

    %% Database seeding relation
    DB_CONF -->|"seed data"| SEED
    SEED -->|"populates"| SQL1
    SEED -->|"populates"| SQL2

    %% Click Events
    click FC "https://github.com/andrejrs/php-mvc-example/blob/master/public/index.php"
    click AL "https://github.com/andrejrs/php-mvc-example/blob/master/src/Psr4AutoloaderClass.php"
    click REQ "https://github.com/andrejrs/php-mvc-example/blob/master/src/Request.php"
    click ROT "https://github.com/andrejrs/php-mvc-example/blob/master/src/Router.php"
    click VAL "https://github.com/andrejrs/php-mvc-example/blob/master/src/Validator.php"
    click ERR "https://github.com/andrejrs/php-mvc-example/blob/master/src/Error.php"
    click CONT "https://github.com/andrejrs/php-mvc-example/blob/master/src/Controller.php"
    click MOD "https://github.com/andrejrs/php-mvc-example/blob/master/src/Model.php"
    click VW "https://github.com/andrejrs/php-mvc-example/blob/master/src/View.php"
    click ROUTE "https://github.com/andrejrs/php-mvc-example/blob/master/App/Routes.php"
    click NSPACE "https://github.com/andrejrs/php-mvc-example/blob/master/App/Namespaces.php"
    click AC "https://github.com/andrejrs/php-mvc-example/blob/master/App/Controllers/Home.php"
    click CUST "https://github.com/andrejrs/php-mvc-example/blob/master/App/Models/Customer.php"
    click ORD "https://github.com/andrejrs/php-mvc-example/blob/master/App/Models/Order.php"
    click ORD_ITM "https://github.com/andrejrs/php-mvc-example/blob/master/App/Models/OrderItem.php"
    click STAT "https://github.com/andrejrs/php-mvc-example/blob/master/App/Models/Statistic.php"
    click PAGE "https://github.com/andrejrs/php-mvc-example/blob/master/App/Views/Page/home.php"
    click GEN "https://github.com/andrejrs/php-mvc-example/blob/master/App/Views/Template.php"
    click ERRVIEW "https://github.com/andrejrs/php-mvc-example/blob/master/App/Views/error.php"
    click APP_CONF "https://github.com/andrejrs/php-mvc-example/blob/master/Config/Application.php"
    click DB_CONF "https://github.com/andrejrs/php-mvc-example/blob/master/Config/Database.php"
    click SEED "https://github.com/andrejrs/php-mvc-example/blob/master/database/OrderSeeder.php"
    click SQL1 "https://github.com/andrejrs/php-mvc-example/blob/master/database/dumps/dump_2018-02-24.sql"
    click SQL2 "https://github.com/andrejrs/php-mvc-example/blob/master/database/dumps/dump_2018-03-01.sql"
    click HTACCESS "https://github.com/andrejrs/php-mvc-example/blob/master/public/.htaccess"
    click CSS "https://github.com/andrejrs/php-mvc-example/blob/master/public/css/cover.css"
    click DASH "https://github.com/andrejrs/php-mvc-example/blob/master/public/js/dashboard.js"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef core fill:#FFA07A,stroke:#333,stroke-width:2px;
    classDef business fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef config fill:#FFE4B5,stroke:#333,stroke-width:2px;
    classDef database fill:#FFA500,stroke:#333,stroke-width:2px;
    classDef assets fill:#87CEEB,stroke:#333,stroke-width:2px;
    classDef routing fill:#FFD700,stroke:#333,stroke-width:2px;
```
