Structure a PHP project without a framework

Advanced  Nov 20, 2026

A folder layout and a few habits that keep a growing PHP site tidy, secure and easy to maintain.

Structure a PHP project without a framework

You do not need a big framework to write clean code. You need a layout and some rules.

The steps of this tutorial at a glance.
The steps of this tutorial at a glance.

A layout that scales

project/
├─ public/            <- the only folder the web server exposes
│  ├─ index.php       <- front controller
│  └─ assets/         <- css, js, images
├─ app/
│  ├─ Controllers/    <- decide what to do
│  ├─ Models/         <- talk to the database
│  └─ Views/          <- HTML templates
├─ config/            <- settings (never in public/)
├─ storage/           <- logs, cache, private uploads
└─ vendor/            <- Composer packages

Putting config, app and storage outside public/ means a mistake in server settings cannot expose your passwords or code.

A layout that scales
A layout that scales

Front controller

Every request enters public/index.php, which loads the config, finds the matching controller and prints a view.

require __DIR__ . '/../app/bootstrap.php';

$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
(new Router())->dispatch($_SERVER['REQUEST_METHOD'], $path);
How the code in this section fits into the page.
How the code in this section fits into the page.

Separate the concerns

  • Model: SQL and business rules only. No HTML.
  • View: HTML and echo. No SQL. Escape every value.
  • Controller: reads the request, calls the model, picks the view.
The main points of this section.
The main points of this section.

Use Composer autoloading

{ "autoload": { "psr-4": { "App\\": "app/" } } }

Then require 'vendor/autoload.php' loads your classes automatically, with no long list of require lines.

Habits that pay off

  • One place for configuration; one place for the database connection.
  • Prepared statements everywhere; h() for every output.
  • Log errors to a file in storage/, and show visitors a friendly page.
  • Strict types: declare(strict_types=1); at the top of files.
  • Write down how to install and update the project (a README).
The main points of this section.
The main points of this section.

When to adopt a framework

If you keep rebuilding routing, validation, authentication and migrations, a framework such as Laravel or Symfony saves time. Learn the plain version first so you understand what the framework does for you.

When to adopt a framework
When to adopt a framework

More tutorials