Project Structure

MVC & Backend Development

Lesson 2 20 min Free Preview

Project Structure

Organizing your MVC application

MVC Project Structure

A well-organized project structure is crucial for maintainability. This lesson covers the standard directory layout used in professional PHP applications.

💡 Pro Tip

Ginto AI follows this exact structure, so your knowledge transfers directly to real-world projects built with the platform.

Recommended Directory Layout

project/
├── src/                          # Application source code
│   ├── Controllers/              # Handle HTTP requests
│   │   ├── HomeController.php
│   │   ├── UserController.php
│   │   └── Api/
│   │       └── ApiController.php
│   ├── Models/                   # Data and business logic
│   │   ├── User.php
│   │   ├── Post.php
│   │   └── Comment.php
│   ├── Views/                    # Templates and layouts
│   │   ├── layouts/
│   │   │   └── main.php
│   │   ├── home/
│   │   │   └── index.php
│   │   └── user/
│   │       ├── index.php
│   │       └── show.php
│   ├── Routes/                   # Route definitions
│   │   ├── web.php
│   │   └── api.php
│   ├── Middleware/               # Request/Response filters
│   │   ├── AuthMiddleware.php
│   │   └── CorsMiddleware.php
│   └── Core/                     # Framework core
│       ├── Router.php
│       └── Database.php
├── public/                       # Web-accessible files
│   ├── index.php                 # Single entry point
│   └── assets/
│       ├── css/
│       ├── js/
│       └── images/
├── config/                       # Configuration files
│   └── database.php
├── storage/                      # Generated files
│   ├── logs/
│   └── cache/
├── vendor/                       # Composer dependencies
├── .env                          # Environment variables
└── composer.json                 # Dependencies

Key Principles

🚪 Single Entry Point

All requests go through public/index.php. This allows centralized security and routing.

📦 PSR-4 Autoloading

Use Composer's autoloader. No more require statements everywhere.

🔐 Environment Config

Keep secrets in .env files. Never commit credentials to Git.

📁 Asset Organization

Static files in public/assets/. Version for cache busting.

The Entry Point: index.php

<?php
// public/index.php

// Load Composer autoloader
require __DIR__ . '/../vendor/autoload.php';

// Load environment variables
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->load();

// Initialize the application
$app = new App\Core\Application();

// Load routes
require __DIR__ . '/../src/Routes/web.php';

// Handle the request
$app->run();

Composer Configuration

{
    "name": "yourapp/project",
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "require": {
        "php": "^8.1",
        "catfan/medoo": "^2.1",
        "vlucas/phpdotenv": "^5.5"
    }
}

Environment Variables

# .env.example
APP_NAME="My Application"
APP_ENV=local
APP_DEBUG=true

DB_HOST=localhost
DB_DATABASE=myapp
DB_USERNAME=root
DB_PASSWORD=

# Never commit .env with real values!

Creating Your First Project

# Create project directory
mkdir myproject && cd myproject

# Initialize Composer
composer init

# Install dependencies
composer require catfan/medoo vlucas/phpdotenv

# Create directory structure
mkdir -p src/{Controllers,Models,Views,Routes,Core}
mkdir -p public/assets/{css,js,images}
mkdir -p config storage/{logs,cache}

# Create entry point
touch public/index.php

# Set permissions
chmod -R 775 storage

🚀 Next Steps

In the next lesson, you'll learn how to build Models with the Medoo ORM for clean database operations.