Routing Basics
Routing in web applications is the process of determining how incoming URLs are mapped to specific code that handles the request, usually by directing them to the appropriate controller and action.
When a user visits a web page, the browser sends a request to your server. In an MVC application, routing is what decides which controller and action should handle that request. Instead of having a separate PHP file for every page, you use a single entry point (like index.php) and a router to interpret the URL. The router directs requests to the correct controller and action, keeping your application organized and easy to expand.
index.php
controllers/HomeController.php
controllers/AboutController.php
1234567891011121314151617<?php // A very simple router example // Get the requested URL path $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); // Remove leading slash $path = ltrim($uri, '/'); // Map URLs to controller files if ($path === 'home') { require 'controllers/HomeController.php'; } elseif ($path === 'about') { require 'controllers/AboutController.php'; } else { echo "404 Not Found"; }
In this example, the router checks the URL path and includes the correct controller file. If the user visits /home, the router includes HomeController.php; if the user visits /about, it includes AboutController.php. If the route does not match any known path, it displays a simple 404 message. This approach keeps your logic separated and makes it easier to manage as your application grows.
The router works by parsing the incoming URL, extracting the path, and then using conditional logic to determine which controller to include. This approach means you do not need a separate PHP file for every possible page, and you can centralize your application logic.
index.php
controllers/UserController.php
controllers/EditPostController.php
1234567891011121314151617181920212223242526<?php // Improved router with dynamic parameters $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $path = ltrim($uri, '/'); // Match routes with dynamic parameters, e.g., /user/42 $routes = [ '#^user/(\d+)$#' => 'UserController.php', '#^post/(\d+)/edit$#' => 'EditPostController.php', ]; $matched = false; foreach ($routes as $pattern => $controller) { if (preg_match($pattern, $path, $matches)) { // Pass dynamic parameters to the controller $_GET['param'] = $matches[1] ?? null; require "controllers/$controller"; $matched = true; break; } } if (!$matched) { echo "404 Not Found"; }
Routing gives your MVC application flexibility because you can easily add, change, or remove routes without rearranging your file structure. You can define how URLs look, make them user-friendly, and pass necessary data to controllers in a consistent way. This separation of concerns makes your application easier to maintain and extend.
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Can you explain how to set up a basic router in PHP?
What are some best practices for organizing controllers in an MVC application?
How can I handle dynamic routes with parameters in this routing setup?
Awesome!
Completion rate improved to 6.67
Routing Basics
Swipe um das Menü anzuzeigen
Routing in web applications is the process of determining how incoming URLs are mapped to specific code that handles the request, usually by directing them to the appropriate controller and action.
When a user visits a web page, the browser sends a request to your server. In an MVC application, routing is what decides which controller and action should handle that request. Instead of having a separate PHP file for every page, you use a single entry point (like index.php) and a router to interpret the URL. The router directs requests to the correct controller and action, keeping your application organized and easy to expand.
index.php
controllers/HomeController.php
controllers/AboutController.php
1234567891011121314151617<?php // A very simple router example // Get the requested URL path $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); // Remove leading slash $path = ltrim($uri, '/'); // Map URLs to controller files if ($path === 'home') { require 'controllers/HomeController.php'; } elseif ($path === 'about') { require 'controllers/AboutController.php'; } else { echo "404 Not Found"; }
In this example, the router checks the URL path and includes the correct controller file. If the user visits /home, the router includes HomeController.php; if the user visits /about, it includes AboutController.php. If the route does not match any known path, it displays a simple 404 message. This approach keeps your logic separated and makes it easier to manage as your application grows.
The router works by parsing the incoming URL, extracting the path, and then using conditional logic to determine which controller to include. This approach means you do not need a separate PHP file for every possible page, and you can centralize your application logic.
index.php
controllers/UserController.php
controllers/EditPostController.php
1234567891011121314151617181920212223242526<?php // Improved router with dynamic parameters $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $path = ltrim($uri, '/'); // Match routes with dynamic parameters, e.g., /user/42 $routes = [ '#^user/(\d+)$#' => 'UserController.php', '#^post/(\d+)/edit$#' => 'EditPostController.php', ]; $matched = false; foreach ($routes as $pattern => $controller) { if (preg_match($pattern, $path, $matches)) { // Pass dynamic parameters to the controller $_GET['param'] = $matches[1] ?? null; require "controllers/$controller"; $matched = true; break; } } if (!$matched) { echo "404 Not Found"; }
Routing gives your MVC application flexibility because you can easily add, change, or remove routes without rearranging your file structure. You can define how URLs look, make them user-friendly, and pass necessary data to controllers in a consistent way. This separation of concerns makes your application easier to maintain and extend.
Danke für Ihr Feedback!