Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: None  |  size: 2.27 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. namespace Framework\Routing;
  4. use \Framework as Core;
  5.  
  6. final class Router
  7. {
  8.     static public function ParseURI()
  9.     {
  10.         $Config     = new Core\Config('Routing');
  11.  
  12.         $Action     = $Config -> Action;
  13.         $Controller = $Config -> Controller;
  14.         $Language   = $Config -> Language;
  15.         $Prefix     = $Config -> Prefix;
  16.  
  17.         $FixURI     = false;
  18.         $Parameters = array();
  19.  
  20.         $Pattern    = '|^$PREFIX$(?:/([a-z]{2})(?=/))?(?:/([a-z]+)(?:/([a-z]+)(?:/([a-z0-9_/-]+))?)?)?|';
  21.         $Subject    = strtolower($_SERVER['REQUEST_URI']);
  22.         $Results    = array();
  23.  
  24.         $Pattern    = str_replace('$PREFIX$', $Config -> Prefix, $Pattern);
  25.  
  26.         if(substr($Subject, 0, strlen($Prefix)) == $Prefix)
  27.             $Subject = substr($Subject, strlen($Prefix));
  28.  
  29.         preg_match_all($Pattern, $Subject, $Results, PREG_SET_ORDER);
  30.  
  31.         $Results = $Results[0];
  32.  
  33.         if(! empty($Results[1]))
  34.             $Language = $Results[1];
  35.         else
  36.             $FixURI = true;
  37.  
  38.         if(! empty($Results[2]))
  39.             $Controller = ucfirst($Results[2]);
  40.         else
  41.             $FixURI = true;
  42.  
  43.         if(! empty($Results[3]))
  44.             $Action = ucfirst($Results[3]);
  45.         else
  46.             $FixURI = true;
  47.  
  48.         if(isset($Results[4]))
  49.         {
  50.             $ParamStr   = trim($Results[4], '/');
  51.             $Parameters = explode('/', $ParamStr);
  52.         }
  53.  
  54.         if($FixURI == false)
  55.         {
  56.             if(isset($Config -> Map[$Controller . '/' . $Action]))
  57.             {
  58.                 $Redirection = $Config -> Map[$Controller . '/' . $Action];
  59.                 self::FixURI($Prefix, $Language, $Controller, $Action, $Parameters);
  60.             }
  61.  
  62.             return new Request($Action, $Controller, $Language, $Parameters);
  63.         }
  64.         else
  65.         {
  66.             self::FixURI($Prefix, $Language, $Controller, $Action, $Parameters);
  67.         }
  68.     }
  69.  
  70.     static private function FixURI($Prefix, $Language, $Controller, $Action, $Parameters)
  71.     {
  72.         $URI = 'http://' . ROOT_WWW . $Prefix . '/' . $Language . '/'
  73.              . $Controller . '/' . $Action;
  74.  
  75.         if(count($Parameters) > 0)
  76.             $URI .= '/' . implode('/', $Parameters);
  77.  
  78.         header('HTTP/1.1 301 Moved Permanently');
  79.         header('Location: ' . strtolower($URI));
  80.  
  81.         exit;
  82.     }
  83. }