📖 Structuring the PHP App MVC-style

What Is MVC?

MVC stands for Model-View-Controller, a structure that separates concerns in a web application:

MVC concept
MVC separates responsibilities between data, interface, and logic
Model
Handles data access and validation — includes queries and updates (e.g., models/PlaylistModel.php).
View
Displays user-facing content — forms, pages, and reusable layout parts (e.g., views/PlaylistView.php).
Controller
Directs traffic — processes user input, decides what data to use, and which view to show (e.g., controllers/PlaylistController.php).

Standard Project Structure

This folder layout promotes reuse and clarity for every part of your app:

project-root/
├── config/
├── controllers/
│   └── PlaylistController.php     ← handles user requests and logic
├── models/
│   ├── db_connect.php             ← database connection script
│   └── PlaylistModel.php          ← database querys and logic
├── views/
│   ├── PlayListView.php           ← view file that displays playlists
│   └── partials/
│       ├── header.php             ← site header
│       └── footer.php             ← site footer
└── playlist.php                   ← entry point (e.g., profile.php, login.php, register.php)

💡 Tip: Action views like show.php, edit.php, or create.php represent distinct interface behaviors within a feature folder (e.g., views/profile/).

What Goes Where?

playlist.php
Entry point triggered by the user — typically routes logic to a controller or loads a view directly.
config/
Includes global setup files such as init.php for sessions or environment variables.
controllers/
Manages page logic and responds to form submissions or other user input.
models/
Handles database communication — inserts, queries, and validation functions.
views/
Contains the UI pages users interact with, organized by feature or layout section.
partials/
Holds shared UI components like header.php and footer.php.

Setting Up Reusable Includes

Use include or require to bring in reusable layout components. Move your standard header and footer into views/partials/ and include them across your view files.

If you're using a single template.php, consider breaking it up into dedicated partials for better flexibility and reuse.

💡 Tip: Use filenames that clearly match intent. For example, views/profile/show.php should only display data — not handle logic or update operations.

Application Flow Example

This sample flow illustrates how a single user request is processed:

register.php
Acts as a request handler. It includes setup and controller files, then calls a function like register_user() to process the request. This file does not contain logic itself — it delegates to the controller.
controllers/UserController.php
Processes registration logic and either renders a view or calls the model.
models/UserModel.php
Performs data validation and inserts new user record into the database.
views/profile/show.php
Displays the updated user profile after registration is complete.

Last updated: August 6, 2025 at 2:39 PM