Dynamic Routing in PHP: How It Works and Why It’s the Core of Every Dynamic Website
🧭 Introduction
Every modern website needs a dynamic structure. This means that when a user visits a specific page, the server must know what content to load, what logic to execute, and what layout to display.
This is made possible through a mechanism called the router.
But what exactly is routing?
And how can we build an effective routing system using pure PHP, without relying on a framework?
📌 What Is Routing?
Routing is the mechanism that analyzes the URL requested by a user and determines which piece of PHP code should be executed.
It acts like a switchboard: it receives incoming requests and directs them to the right controller.
Example:
/about → AboutController@index
/view?slug=x → ViewController@index
/login → LoginController@index
🧱 Project Structure Overview
A well-designed router typically includes:
routes.php: a map between URL paths and controller methodsindex.php: the main entry point that parses the URI and manages requests- Controllers: PHP classes that handle logic and data
- Views: HTML templates that render content to the user
⚙️ How It Works
1. Route Map
In routes.php, you define a simple route map:
$routes = [
"/" => "HomeController@index",
"/about" => "AboutController@index",
"/contact" => "ContactController@index",
"/view" => "ViewController@index",
];
2. URI Parsing and Dispatch
In index.php, the system analyzes the requested URI:
$requestUri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$requestUri = $requestUri === '' ? '/' : $requestUri;
foreach ($routes as $pattern => $handler) {
if ($pattern === $requestUri) {
list($controller, $method) = explode('@', $handler);
require_once "controllers/$controller.php";
$instance = new $controller();
$instance->$method();
return;
}
}
http_response_code(404);
echo "Page not found.";
3. Controller
Here’s a basic controller example:
class ViewController {
public function index() {
$slug = $_GET['slug'] ?? '';
if (!$slug) {
echo "Missing slug.";
return;
}
// Fetch data from the database...
$content = "Article text with slug: $slug";
// Load the view
include 'views/view.php';
}
}
4. View
view.php is the template that displays the content:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Article</title>
</head>
<body>
<h1>Article</h1>
<div>
<?= $content ?>
</div>
</body>
</html>
🧠 Why Use a Custom Router?
- ✅ Full control over routing logic
- ✅ Adding new pages is simple: just one line in the route map, one controller method
- ✅ Easy management of sessions, roles, and authentication
- ✅ Much lighter than a full framework
🔮 Advanced Enhancements
To take it to the next level, you can:
- Use regex patterns in routes (e.g.,
/view/([a-z0-9-]+)) - Support multiple HTTP methods (GET, POST)
- Handle dynamic URL parameters
- Add middleware (authentication, filters, etc.)
🧭 Conclusion
Routing is the heart of every dynamic website.
Building it in a modular, clear, and scalable way ensures readable, maintainable, and powerful code.
Whether you’re building a simple blog or a full platform, a robust routing system is what turns a static site into a living one.
