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.

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:
- 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.
- Views: The user interface. These are your templates (HTML files) that display data passed to them. They should contain minimal logic.
- 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:
TEXTmy-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.
| Directory | Responsibility |
|---|---|
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
- Create the directory structure listed above on your local machine.
- Move your previous PHP scripts into the
src/folder. - Create a
src/Views/home.phpfile containing simple HTML output. - Update your
public/index.phpto include (require) that view file.
Common Pitfalls
- Publicly exposing
src/: Ensure your web server (like Apache or Nginx) points its "Document Root" to thepublic/folder, not themy-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.
Work with me

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs — the backend engine for your app, mobile client, or SaaS. Built by an API specialist.

FilamentPHP Admin Panel & Dashboard Development
A powerful admin panel for your Laravel app — built with FilamentPHP so you can manage everything without touching the database.


