require_once INCLUDE_DIR . '/Twig/lib/Twig/Autoloader.php'; // register the Twig Autoloader Twig_Autoloader::register(); // Create a new instance of Router $router = new Router(); // Get an instance of Dispatcher $dispatcher = new Dispatcher(); $dispatcher->setSuffix('Controller'); $dispatcher->setClassPath(CONTROLLER_DIR); // Set up your default route: $default_route = new Route('/'); $default_route->setMapClass('default')->setMapMethod('index'); $router->addRoute('default', $default_route); $url = urldecode($_SERVER['REQUEST_URI']); try { $found_route = $router->findRoute($url); $dispatcher->dispatch($found_route); } catch (RouteNotFoundException $e) { PageError::show('404', $url); } catch (badClassNameException $e) { PageError::show('400', $url); } catch (classFileNotFoundException $e) { PageError::show('500', $url); } catch (classNameNotFoundException $e) { PageError::show('500', $url); } catch (classMethodNotFoundException $e) { PageError::show('500', $url); } catch (classNotSpecifiedException $e) { PageError::show('500', $url); } catch (methodNotSpecifiedException $e) { PageError::show('500', $url);
<?php function __autoload($c) { require_once "classes/" . $c . ".php"; } // should probably be loaded from a DB somewhere $routes = ["home" => ["body" => "body.partial.html", "title" => "Homepage!"], "contact" => ["body" => "contact.partial.html", "title" => "Contact pagina!", "header" => "header.partial.html"], "404" => ["body" => "404.partial.html", "title" => "Oops! Page not found"]]; $router = new Router($routes); // Router if (isset($_GET['r'])) { // route is requested $route = $_GET['r']; // find the requested route // if it doesnt exist, this will be 404 $router->findRoute($route); } else { // get the homepage $router->findRoute("home"); } // create a new page with default values (html path, default header, default footer) for the current route $page = new HTMLBuilder("html/", "header.partial.html", "footer.partial.html", $router); $page->buildPage();
public function testQueryParameters() { $router = new Router(); $route = new Route("/2008-08-01/Accounts/:id/IncomingPhoneNumbers"); $route->setMapClass('IncomingPhoneNumbers')->setMapMethod('list'); $route->addDynamicElement(':id', ':id'); $router->addRoute('test', $route); $found = $router->findRoute('/2008-08-01/Accounts/1/IncomingPhoneNumbers?a=1&b=2'); $this->assertSame($found, $route); }