- <?php
- namespace Framework\Routing;
- use \Framework as Core;
- final class Router
- {
- static public function ParseURI()
- {
- $Config = new Core\Config('Routing');
- $Action = $Config -> Action;
- $Controller = $Config -> Controller;
- $Language = $Config -> Language;
- $Prefix = $Config -> Prefix;
- $FixURI = false;
- $Parameters = array();
- $Pattern = '|^$PREFIX$(?:/([a-z]{2})(?=/))?(?:/([a-z]+)(?:/([a-z]+)(?:/([a-z0-9_/-]+))?)?)?|';
- $Subject = strtolower($_SERVER['REQUEST_URI']);
- $Results = array();
- $Pattern = str_replace('$PREFIX$', $Config -> Prefix, $Pattern);
- if(substr($Subject, 0, strlen($Prefix)) == $Prefix)
- $Subject = substr($Subject, strlen($Prefix));
- preg_match_all($Pattern, $Subject, $Results, PREG_SET_ORDER);
- $Results = $Results[0];
- if(! empty($Results[1]))
- $Language = $Results[1];
- else
- $FixURI = true;
- if(! empty($Results[2]))
- $Controller = ucfirst($Results[2]);
- else
- $FixURI = true;
- if(! empty($Results[3]))
- $Action = ucfirst($Results[3]);
- else
- $FixURI = true;
- if(isset($Results[4]))
- {
- $ParamStr = trim($Results[4], '/');
- $Parameters = explode('/', $ParamStr);
- }
- if($FixURI == false)
- {
- if(isset($Config -> Map[$Controller . '/' . $Action]))
- {
- $Redirection = $Config -> Map[$Controller . '/' . $Action];
- self::FixURI($Prefix, $Language, $Controller, $Action, $Parameters);
- }
- return new Request($Action, $Controller, $Language, $Parameters);
- }
- else
- {
- self::FixURI($Prefix, $Language, $Controller, $Action, $Parameters);
- }
- }
- static private function FixURI($Prefix, $Language, $Controller, $Action, $Parameters)
- {
- $URI = 'http://' . ROOT_WWW . $Prefix . '/' . $Language . '/'
- . $Controller . '/' . $Action;
- if(count($Parameters) > 0)
- $URI .= '/' . implode('/', $Parameters);
- header('HTTP/1.1 301 Moved Permanently');
- header('Location: ' . strtolower($URI));
- exit;
- }
- }