What is a Front Controller design pattern?

A Front Controller is a single entry point (usually one PHP file like index.php) that handles all incoming HTTP requests to your application.

Instead of having multiple entry points (e.g., one script per page), you have one centralized file that:

  1. Receives the request
  2. Initializes the framework
  3. Routes the request to the right controller or action
  4. Returns the response

Example in Laravel or CodeIgniter:

  • The public/index.php file is the front controller.
  • All routes (e.g., /about, /contact, etc.) are routed through this one file.
  • URL rewriting (via .htaccess or Nginx rules) sends every request to index.php.

🎯 Why Use the Front Controller Pattern?

  • Centralized control over request handling
  • Easier to implement routing, middleware, logging, authentication
  • More secure, since it avoids direct access to internal files
  • Consistent request lifecycle (start → middleware → controller → response)

What happens if we don’t use the Front Controller pattern?

If you don’t use the front controller pattern, you fall back to what’s called the Page Controller pattern — where each page has its own PHP script as an entry point.

🔧 Example:

/about.php
/contact.php
/products.php

Each of these files:

  • Handles its own logic
  • Loads its own models and views
  • Responds to a specific URL

🆚 Front Controller vs Page Controller

FeaturePage ControllerFront Controller
Entry PointOne script per page (about.php, etc.)One script (index.php) for all routes
RoutingManual via filenames or .php URLsCentralized and dynamic
ReusabilityLow (duplicate code)High (middleware, unified logic)
MaintainabilityHarder in large appsMuch easier in large apps
Middleware SupportNot built-inEasy to add (auth, logging, etc.)
Modern Framework UseRarely usedUsed by Laravel, Symfony, CodeIgniter, etc.