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:
- Receives the request
- Initializes the framework
- Routes the request to the right controller or action
- Returns the response
Example in Laravel or CodeIgniter:
- The
public/index.phpfile is the front controller. - All routes (e.g.,
/about,/contact, etc.) are routed through this one file. - URL rewriting (via
.htaccessor Nginx rules) sends every request toindex.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
| Feature | Page Controller | Front Controller |
|---|---|---|
| Entry Point | One script per page (about.php, etc.) | One script (index.php) for all routes |
| Routing | Manual via filenames or .php URLs | Centralized and dynamic |
| Reusability | Low (duplicate code) | High (middleware, unified logic) |
| Maintainability | Harder in large apps | Much easier in large apps |
| Middleware Support | Not built-in | Easy to add (auth, logging, etc.) |
| Modern Framework Use | Rarely used | Used by Laravel, Symfony, CodeIgniter, etc. |
