/** * 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()); }
<?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(); }
<?php require_once "vendor/autoload.php"; require_once "app/config/globals.php"; require_once "app/config/email.php"; require_once ROUTES_FILE; $router = new Framework\Router($_SERVER, $__ROUTES, $_POST); include $router->Run();
<?php session_start(); require_once "./app/init.php"; require_once "./vendor/autoload.php"; require_once "./app/autoloader.php"; require_once "app/Core/Framework/Logger.php"; require_once "app/Core/Framework/Router.php"; $controllerName = "Default"; if (array_key_exists('c', $_GET) && $_GET['c']) { $controllerName = $_GET['c']; } $action = "default"; if (array_key_exists('a', $_GET) && $_GET['a']) { $action = $_GET['a']; } try { $logger = new \Framework\Logger(__DIR__ . "/logs/logs.txt", \Framework\Logger::LOG_LEVEL_DEBUG); $router = new \Framework\Router($logger); echo $router->renderAction($controllerName, $action); } catch (Exception $e) { $logger->alert($e->getMessage()); echo "<pre>" . $e->getMessage() . "</pre>"; echo "<pre>" . $e->getTraceAsString() . "</pre>"; }