Back to Blog
Lesson 12 of the PHP: Modern PHP from the Ground Up course
PHPJuly 30, 20263 min read

Project Structure Strategy: Organizing Your PHP MVC Application

Learn to organize your PHP code using MVC architecture. We’ll cover project structure and file organization to keep your app maintainable and professional.

PHPMVCproject structurebackend developmentweb development
A laptop screen showing a code editor with visible programming code in a dimly lit environment.

Previously in this course, we mastered defining custom functions to make our code more modular. Now that we can write reusable logic, we need a professional way to store that code.

As your application grows beyond a single script, throwing every file into one folder becomes a nightmare. To keep your project maintainable, we must adopt a formal project structure. We will implement the MVC architecture (Model-View-Controller), the industry standard for separating concerns in web applications.

The MVC Architecture from First Principles

MVC splits your application into three distinct layers:

  1. Models: Handle your data and business logic. They don't care how the data is displayed; they only care how it is fetched, saved, or calculated.
  2. Views: The user interface. These are your templates (HTML files) that display data passed to them. They should contain minimal logic.
  3. Controllers: The "traffic cops." They receive the user's request, talk to the Model to get data, and pass that data to the View to be rendered.

By decoupling these, you can change your database (Model) without touching your design (View), or change your design without breaking your logic (Controller).

Establishing Your Project Root

Before writing code, we need a clean workspace. Create a new folder for your project, for example, my-app. Inside this folder, we will create a structure that separates "public" files (accessible by the browser) from "private" application files (hidden from the browser).

Here is the professional structure we will build:

TEXT
my-app/
├── public/           # Only files here are publicly accessible
│   └── index.php     # The entry point
├── src/              # Your application logic
│   ├── Controllers/
│   ├── Models/
│   └── Views/
└── config/           # Configuration files

The Entry Point: index.php

In modern web development, we use a "Front Controller" pattern. Instead of the user visiting about.php or contact.php, they visit index.php. This file acts as the gateway for every request, where we can perform global setup tasks like loading dependencies or handling routing.

Create public/index.php with this basic structure:

PHP
<?php
#6A9955">// public/index.php

#6A9955">// 1. Setup: Load config and autoloader(we'll cover these later)
require_once __DIR__ . '/../config/settings.php';

#6A9955">// 2. Routing: Identify which Controller to call
$page = $_GET['page'] ?? 'home';

#6A9955">// 3. Execution: Logic will eventually go here
echo "Welcome to the application!";

Why This Organization Matters

If you’ve read about mastering modular directory structures, you know that consistent file organization is key to scaling. By keeping our src/ directory outside of the web server's public path, we ensure that users cannot accidentally download our raw PHP code or configuration files.

DirectoryResponsibility
public/Web server root; contains assets (CSS/JS) and the entry point.
src/Controllers/Receives requests and orchestrates logic.
src/Models/Interacts with data sources (like databases).
src/Views/HTML templates for the final output.

Hands-on Exercise

  1. Create the directory structure listed above on your local machine.
  2. Move your previous PHP scripts into the src/ folder.
  3. Create a src/Views/home.php file containing simple HTML output.
  4. Update your public/index.php to include (require) that view file.

Common Pitfalls

  • Publicly exposing src/: Ensure your web server (like Apache or Nginx) points its "Document Root" to the public/ folder, not the my-app/ root. This prevents users from accessing your source code via a URL.
  • Deep nesting: Don't over-engineer your folders. If you only have three files, don't create ten subdirectories. Keep it simple until the complexity demands more structure.
  • Hardcoding paths: Always use __DIR__ to define paths relative to the current file. This makes your project portable across different operating systems.

Recap

A professional project structure is the foundation of a scalable application. By organizing our code into Controllers, Models, and Views, we enforce clear boundaries. By using a single public/index.php as our entry point, we gain a centralized location to manage incoming requests.

Adopting these habits now prevents the "spaghetti code" that makes debugging impossible as your task management API grows in complexity.

Up next: We will learn how to handle user data dynamically by processing GET requests using our new directory structure.

Similar Posts