composer and ecosystem

Common PHP Project Directory Structure

PHP projects vary, but a clear directory layout makes ownership visible. Application source, tests, configuration, public entry points, templates, migrations, runtime storage, and vendor code should not be mixed together.

Working Knowledge

  • Keep the web-server document root narrow, commonly public/.
  • Place application classes under a namespaced source directory such as src/ or app/.
  • Keep tests separate from production source.
  • Keep writable runtime directories such as logs and cache outside public serving paths.
  • Treat vendor/ as generated dependency output.

A Conventional Layout

project/
  bin/                 command-line entry points
  config/              application configuration
  migrations/          versioned database changes
  public/
    index.php          web entry point
  src/                 application classes
  templates/           server-rendered views
  tests/               automated tests
  var/                 writable cache and logs
  vendor/              generated Composer dependencies
  composer.json
  composer.lock

The web server should expose public/, not the repository root. That keeps source, configuration, logs, and dependency metadata away from direct HTTP access.

Follow The Existing Framework

Laravel commonly uses app/, routes/, resources/, and storage/. Symfony commonly uses src/, config/, templates/, and var/. The exact folders differ, but the responsibilities remain recognisable.

In Application Work

Follow the repository's existing layout unless a change has a clear benefit. A consistent ordinary layout is easier to operate than a novel one.

What To Check

Before moving on, make sure you can identify source, tests, configuration, public entry points, migrations, writable storage, and generated dependencies in an unfamiliar project.

Practice

Practice: Build A Project Layout

Create the conventional application directories in the practice Composer project and explain what belongs in each one.

Requirements

  • Include source, tests, config, public entry point, migrations, and runtime storage.
  • Mark generated directories.
  • Keep private files outside public/.
  • Connect namespaces to source paths.

Add .gitignore entries for generated dependencies and writable runtime output. Confirm the web-server document root would be public/, not the repository root.

Show solution
bin/
config/
migrations/
public/index.php
src/
templates/
tests/
var/
composer.json
composer.lock

Add vendor/ and writable files under var/ to .gitignore. Serve only public/. Keep source, configuration, dependencies, migrations, and runtime output outside the document root.