- parse_ini_string: documentation ( source)
<?php
$userSpecifiedPath = '/news'; // Change me!
// Don't edit below ===============================================
// ================================================================
// Simple config example
// ================================================================
$routerConfig = <<<CONFIG
[/]
Controller = "HomepageController"
[404]
Controller = "404Controller"
CONFIG;
// ================================================================
// Simple router example
// ================================================================
class Router {
private $routes;
public function __construct($routes) {
$this->routes = $routes;
}
public function getControllerFromPath($path) {
if(array_key_exists($path, $this->routes) === true) {
return $this->routes[$path];
} elseif(array_key_exists('404', $this->routes) === true) {
return $this->routes['404'];
}
}
}
// ================================================================
// Sample output
// ================================================================
$routes = parse_ini_string($routerConfig, true);
$Router = new Router($routes);
$selectedRoute = $Router->getControllerFromPath($userSpecifiedPath);
echo 'Using controller: ' . $selectedRoute['Controller'];