How does the MVC pattern differ from procedural programming when it comes to handling user requests based on parameters like $_GET['page']?
In procedural programming, handling user requests based on parameters like $_GET['page'] typically involves conditional statements scattered throughout the code. On the other hand, the MVC pattern separates concerns by having a dedicated controller to handle user requests and route them to the appropriate actions. This results in a more organized and maintainable codebase.
// Example of handling user requests based on parameters using the MVC pattern
// index.php
$page = isset($_GET['page']) ? $_GET['page'] : 'home';
// Instantiate the controller based on the requested page
switch ($page) {
case 'home':
$controller = new HomeController();
break;
case 'about':
$controller = new AboutController();
break;
default:
$controller = new ErrorController();
}
// Call the appropriate action method based on the request
$controller->index();