Example #1
0
 public static function getRoute(Uri $uri)
 {
     $route = new Route();
     $path = self::getRewrittenPath($uri->getPathString());
     // Set the namespace
     if (count($path) > 0) {
         if (array_key_exists($path[0], self::getNamespaces())) {
             $namespace = self::getNamespace($path[0]);
             $namespaceUrl = $path[0] . '/';
             $path = array_slice($path, 1);
         } else {
             $namespace = self::getNamespace('_default');
             $namespaceUrl = '';
         }
     } else {
         header('Location:/' . Config::read('langs.default') . '/');
     }
     $route->setModuleName(current(explode('\\', ltrim($namespace, '\\'))));
     // Cause the maduleName is the first of all namespaces
     // Set the lang
     $langs = Config::read('langs');
     if (count($path) > 0) {
         if (in_array($path[0], $langs['autorized'])) {
             $route->setLang($path[0]);
             $path = array_values(array_slice($path, 1));
         } else {
             header('Location:/' . $namespaceUrl . $langs['default']);
             //.'/'.implode('/', array_slice($path, 1)));
         }
     } else {
         header('Location:/' . $namespaceUrl . $langs['default']);
         //.'/'.implode('/', $path));
     }
     // Set the controller name
     if (count($path) > 0) {
         $controllerName = $namespace . '\\' . ucfirst(strtolower($path[0]));
         $path = array_values(array_slice($path, 1));
     } else {
         $controllerName = $namespace . '\\' . self::$defaultController;
     }
     $route->setControllerName($controllerName);
     // Set the action name
     if (count($path) > 0) {
         $route->setActionName(strtolower($path[0]) . 'Action');
     } else {
         $route->setActionName(self::$defaultAction . 'Action');
     }
     // Set parameters
     $route->setParams(array_slice($path, 1));
     return $route;
     // $pattern = explode("?", $pattern);
     // $pattern = $pattern[0];
     // if($route = self::findPattern(preg_replace("#{([".self::$_regex."]+)}#i", "{}", $pattern), true)) {
     // 	$tab = preg_split("#[{}]#i", $route["pattern"]);
     // 	if(count($tab) > 1) {
     // 		foreach ($tab as $key => $value) {
     // 			if($key%2 == 1)
     // 				$paramsName[] = $value;
     // 			else
     // 				$pattern = str_replace($value, "%", $pattern);
     // 		}
     // 		$paramsValue = explode("%", $pattern);
     // 		foreach ($paramsName as $key => $value)
     // 			$params[$value] = $paramsValue[$key+1];
     // 	}
     // 	else
     // 		$params = array();
     // 	return array("controller" => $route["controller"], "action" => $route["action"], "params" => $params);
     // }
     // else {
     // 	$route = array_values(array_filter(explode("/", $pattern)));
     // 	return array(
     // 			"controller" => (array_key_exists(0, $route)) ? $route[0] : self::$_defaultController,
     // 			"action" => (array_key_exists(1, $route)) ? $route[1] : self::$_defaultAction,
     // 			"params" => array_slice($route, 2),
     // 		);
     // }
 }