1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public function getController(){
$router = new \Library\Router;
$xml = new \DOMDocument;
$xml->load(__DIR__.'/../Applications/'.$this->getName().'/Config/routes.xml');
$routes = $xml->getElementsByTagName('route');
// On parcourt les routes du fichier XML.
foreach ($routes as $route){
$vars = array();
// On regarde si des variables sont présentes dans l'URL.
if ($route->hasAttribute('vars')){
echo 'TRUE';
$vars = explode(',', $route->getAttribute('vars'));
}
// On ajoute la route au routeur.
$router->addRoute(new Route($route->getAttribute('url'), $route->getAttribute('module'), $route->getAttribute('action'), $vars));
}
echo '<br /><pre>';
var_dump($vars);
echo '</pre>';
try{
// On récupère la route correspondante à l'URL.
$matchedRoute = $router->getRoute($this->httpRequest->getURI());
}
catch (\RuntimeException $e){
if ($e->getCode() == \Library\Router::NO_ROUTE){
// Si aucune route ne correspond, c'est que la page demandée n'existe pas.
$this->httpResponse->redirect404();
}
}
// On ajoute les variables de l'URL au tableau $_GET.
$_GET = array_merge($_GET, $matchedRoute->getVars());
// On instancie le contrôleur.
$controllerClass = 'Applications\\'.$this->name.'\\Modules\\'.$matchedRoute->getModule().'\\'.$matchedRoute->getModule().'Controller';
return new $controllerClass($this, $matchedRoute->getModule(), $matchedRoute->getAction());
} |
Partager