コード例 #1
0
 /**
  * Applique la bonne route pour la requete faites par le client.
  */
 public function getController()
 {
     $router = new \Framework\Router();
     $xml = new \DOMDocument();
     $xml->load('../Applications/' . $this->name . '/Config/routes.xml');
     $routes = $xml->getElementsByTagName('route');
     // Note: On parcourt les routes du fichier XML.
     foreach ($routes as $route) {
         $vars = array();
         // Note: On regarde si des variables sont présentes dans l'URL.
         if ($route->hasAttribute('vars')) {
             $vars = explode(',', $route->getAttribute('vars'));
         }
         // Note: On ajoute la route au routeur
         $router->addRoute(new Route($route->getAttribute('url'), $route->getAttribute('module'), $route->getAttribute('action'), $vars));
     }
     try {
         // Note: On récupère la route correspondante à l'URL.
         $matchedRoute = $router->getRoute($this->httpRequest->requestURI());
     } catch (\RuntimeException $e) {
         if ($e->getCode() == \Framework\Router::NO_ROUTE) {
             // Note: Si aucune route ne correspond, c'est que la page demandée n'existe pas.
             $this->httpResponse->redirect404();
         }
     }
     // Note: On ajoute les variables de l'URL au tableau $_GET.
     $_GET = array_merge($_GET, $matchedRoute->vars());
     // Note: On instancie le contrôleur.
     $controllerClass = '\\Applications\\' . $this->name . '\\Modules\\' . $matchedRoute->module() . '\\' . $matchedRoute->module() . 'Controller';
     return new $controllerClass($this, $matchedRoute->module(), $matchedRoute->action());
 }
コード例 #2
0
ファイル: test_router.php プロジェクト: soanni/mvc
<?php

require_once 'autoloader.php';
try {
    $router = new \Framework\Router();
    $router->addRoute(new \Framework\Router\Route\Simple(array("pattern" => ":name/profile", "controller" => "home", "action" => "index")));
    $router->url = "chris/profile";
    $router->dispatch();
} catch (Exception $e) {
    echo $e->getMessage();
}