How can a routing system in PHP be structured to ensure clear and effective handling of arguments and function calls?

To ensure clear and effective handling of arguments and function calls in a routing system in PHP, it is important to use a structured approach such as defining routes with associated controllers and actions. This helps in organizing the code and separating concerns, making it easier to manage and maintain the routing logic.

// Define routes with associated controllers and actions
$routes = [
    '/' => ['controller' => 'HomeController', 'action' => 'index'],
    '/about' => ['controller' => 'AboutController', 'action' => 'index'],
    '/contact' => ['controller' => 'ContactController', 'action' => 'index'],
];

// Get the requested URL
$requestUrl = $_SERVER['REQUEST_URI'];

// Match the requested URL to a route and call the associated controller action
if (array_key_exists($requestUrl, $routes)) {
    $controller = new $routes[$requestUrl]['controller'];
    $action = $routes[$requestUrl]['action'];
    $controller->$action();
} else {
    // Handle 404 error
    echo '404 Page Not Found';
}