/** * This method forward to the action. * * @param object $Router the instance of the Router class. */ public static function run() { $request = new Request(); $request->set(); $results = Route::resolveRoute($request); if (!is_null($results)) { Mvc::setCurrentMvc($results['mvc']); Mvc::setCurrentApp(Framework::app($results['mvc']->appName())); $request = $results['request']; TemplateMapper::templates(); $tpl = TemplateMapper::getTemplate(Mvc::moduleName(), Mvc::controllerName(), Mvc::actionName()); session_start(); //Take out global, not use require_once Mvc::controllerFile(); $controller = Mvc::controllerClassName(); $action = Mvc::actionFunctionName(); self::$runController = new $controller(); self::$runController->setParams($request->params()); self::$runController->setRequest($request); self::$runController->setDefaultModel(); self::$runController->setDefaultView($tpl); //init method acts as a constructor after all the variables being set self::$runController->init(); if (method_exists(self::$runController, $action)) { self::$runController->{$action}(); } else { throw AiryException("AiryMVC: Missing Action Function."); } return self::$runController; session_write_close(); } }
/** * This initializes what framework needs. */ public static function init() { //Get framework application names //Throw out the error if the json cannot be decoded $configArray = json_decode(file_get_contents(Framework::configFile()), TRUE); $names = array(); $apps = array(); $allAppRoutes = array(); foreach ($configArray['%applications'] as $application) { $keys = array_keys($application); $names[] = $keys[0]; $appSettings = $application[$keys[0]]; //server name & document root & relative path $serverName = $appSettings[0]; $docRoot = $appSettings[1]; $pathName = "/"; if (substr($appSettings[2], 0, 1) != "/") { $pathName .= $appSettings[2]; } $app = new Application($keys[0], $serverName, $docRoot, $pathName); //** Get the application's application config $appSpecificConfigArray = NULL; if (!is_null($app->appConfigFile())) { $appSpecificConfigArray = json_decode(file_get_contents($app->appConfigFile()), TRUE); } //Get the framework's application config $finalAppConfigArray = array(); foreach ($configArray["%application_configs"] as $appConfigArray) { if ($appConfigArray["%name"] == $keys[0]) { $finalAppConfigArray = $appConfigArray; } } //Overwrite the application_configs by application's config array if (isset($appSpecificConfigArray['%application_configs']) && isset($appSpecificConfigArray['%application_configs'][0])) { $finalAppConfigArray = array_replace_recursive($finalAppConfigArray, $appSpecificConfigArray['%application_configs'][0]); } $app->setConfig($finalAppConfigArray); $apps[$keys[0]] = $app; //Get all routes from application specific config if (isset($appSpecificConfigArray["%routes"])) { $allAppRoutes = array_replace_recursive($allAppRoutes, $appSpecificConfigArray["%routes"]); } } Framework::$appNames = $names; Framework::$apps = $apps; //process routing table here $table = array(); $routeParams = array(); if (isset($configArray['%routes'])) { //overwrite the routes $allRoutes = $configArray['%routes']; if (!empty($allAppRoutes)) { $allRoutes = array_replace_recursive($configArray['%routes'], $allAppRoutes); } foreach ($allRoutes as $routeUrl => $routeVar) { if (isset($apps[$routeVar[0]])) { $appObj = $apps[$routeVar[0]]; $mvcValue = new MvcValue(); $mvcValue->setNames($appObj->relativePath(), $routeVar[0], $routeVar[1], Route::fromHyphenToCamelCase($routeVar[2], TRUE), Route::fromHyphenToCamelCase($routeVar[3])); $table[$routeUrl] = $mvcValue; if (isset($routeVar[4])) { $routeParams[$routeUrl] = $routeVar[4]; } } } } Route::$routingTable = $table; Route::$routingParams = $routeParams; include "AutoLoader.php"; }
public static function resolveRoute($request) { $matchRoute = NULL; $runMvc = NULL; //get the match URI foreach (Route::routingTable() as $route => $mvc) { $pos = strpos($request->requestURI(), $route); if ($pos !== FALSE && $pos == 0) { $runMvc = $mvc; $matchRoute = $route; break; } } //Has matches of routing table; set the parameters in the request if (!is_null($runMvc) && !is_null($matchRoute)) { $idx = strlen($matchRoute); $restURI = substr($request->requestURI(), $idx); if (substr($restURI, 0, 1) == "/") { $restURI = substr($restURI, 1); } $uriParts = explode("/", $restURI); if (count($uriParts) >= 2) { for ($i = 0; $i < count($uriParts); $i = $i + 2) { $uriParts[$i + 1] = isset($uriParts[$i + 1]) ? $uriParts[$i + 1] : NULL; $request->setParam($uriParts[$i], $uriParts[$i + 1]); $request->setGETParam($uriParts[$i], $uriParts[$i + 1]); } } $routeParameters = Route::routingParams(); if (isset($routeParameters[$matchRoute])) { $useEqual = FALSE; //case #1: ?key1=value1&key2=value2 $options = ltrim($routeParameters[$matchRoute], "?"); $kvs = explode("&", $options); foreach ($kvs as $kv) { $kvParts = explode("=", $kv); if (count($kvParts) > 1) { $request->setParam($kvParts[0], $kvParts[1]); $request->setGETParam($kvParts[0], $kvParts[1]); $useEqual = TRUE; } } //case #2: /key1/value1/key2/value2 if (!$useEqual) { $options = ltrim($routeParameters[$matchRoute], "/"); $elems = explode("/", $options); if (count($elems) > 1) { for ($i = 0; $i < count($elems); $i = $i + 2) { $request->setParam($elems[$i], $elems[$i + 1]); $request->setGETParam($elems[$i], $elems[$i + 1]); } } } } return array("mvc" => $runMvc, "request" => $request); } //No matching route - start from app name; transform directive to MVC and params $trimUri = ltrim($request->requestURI(), "/"); $uelem = explode("/", $trimUri); if (count($uelem) < 4) { //wrong route or no matching at all return NULL; } $app = Framework::app($uelem[0]); //if $app is null, no matching routes if (!is_null($app)) { $mvcVal = new MvcValue(); $mvcVal->setNames($app->relativePath(), $app->name(), $uelem[1], Route::fromHyphenToCamelCase($uelem[2], TRUE), Route::fromHyphenToCamelCase($uelem[3])); for ($i = 4; $i < count($uelem); $i = $i + 2) { $uelem[$i + 1] = isset($uelem[$i + 1]) ? $uelem[$i + 1] : NULL; $request->setParam($uelem[$i], $uelem[$i + 1]); $request->setGETParam($uelem[$i], $uelem[$i + 1]); } return array("mvc" => $mvcVal, "request" => $request); } return NULL; }