public function testExecuteRoute() { $Router = new Router(array()); $route = $Router->parseRoute(); $Router->executeRoute($route); $this->assertEquals($Router->controller->calledAction, 'util'); $this->assertEquals($Router->controller->actionParams, array('router')); }
/** * Starts the PhpBURN Application - Should be called at the index of the application * <code> * PhpBURN::StartApplication(); * </code> */ public static function startApplication() { self::enableAutoload(); // Setting up Controller if (array_search('Controller', get_declared_classes()) == true) { PhpBURN::load('Tools.Util.Router'); include_once SYS_APPLICATION_PATH . DS . 'config' . DS . 'routes.php'; //Define the main route functions $router = new Router($routes); $currentRoute = $router->parseRoute(); if ($currentRoute != false) { $router->executeRoute($currentRoute); } else { @Controller::callErrorPage('404'); } } }
/** * Starts the PhpBURN Application - Should be called at the index of the application * <code> * PhpBURN::StartApplication(); * </code> */ public static function startApplication() { // Setting up Model if (array_search('PhpBURN_Core', get_declared_classes()) == true) { // Adds Models Paths to include Path $packages = PhpBURN_Configuration::getConfig(); foreach ($packages as $package => $configItem) { $includePath = get_include_path(); $separator = strpos($includePath, ':') !== false ? ':' : ';'; set_include_path($includePath . $separator . $configItem->class_path . DS . $package); } } // Setting up Controller if (array_search('Controller', get_declared_classes()) == true) { PhpBURN::load('Tools.Util.Router'); include_once SYS_APPLICATION_PATH . DS . 'config' . DS . 'routes.php'; //Define the main route functions $router = new Router($routes); $currentRoute = $router->parseRoute(); if ($currentRoute != false) { $router->executeRoute($currentRoute); } else { Controller::callErrorPage('404'); } } }
foreach ($data['photos']['photo'] as $photo) { $photos[] = 'http://farm' . $photo['farm'] . '.static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'] . '.jpg'; } return $photos; } else { return []; } } class Router { private static $routes = []; public static function addRoute($request_type, $pattern, $callback) { $pattern = '/^' . str_replace('/', '\\/', $pattern) . '$/'; self::$routes[$request_type][$pattern] = $callback; } public static function executeRoute($url) { $request_type = $_SERVER['REQUEST_METHOD']; foreach (self::$routes[$request_type] as $regex => $function) { if (preg_match($regex, $url, $params)) { array_shift($params); ksort($params, SORT_NATURAL); return call_user_func_array($function, [$params]); } } } } register_shutdown_function(function () { Router::executeRoute($_SERVER['REQUEST_URI']); });
<?php require_once "errors.php"; require_once "Exceptions.php"; require_once "db.php"; require_once 'HttpRequest.php'; require_once 'HttpResponse.php'; require_once 'Authenticator.php'; require_once 'Router.php'; require_once 'Trips.php'; $resp = new HttpResponse(); try { $db = new mysqli($dbhost, $dbuser, $dbpw, $dbdb); $req = new HttpRequest(); $auth = new Authenticator($db, $req); //the router handles all of the other stuff (loading controller etc.) $router = new Router($req, $resp, $db, $auth); $content = $router->executeRoute(); $resp->write($content); $resp->flush(); } catch (Exception $e) { $resp->setStatus(400); $resp->addHeader('error', $e->getMessage()); $resp->write('{"exception" : "' . $e->getMessage() . ' [' . $e->getFile() . '#' . $e->getLine() . ']"}'); $resp->flush(); }