Guest User

Untitled

a guest
Apr 21st, 2013
123
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class RouterException extends Exception {}
  4.  
  5. class Router {
  6.  
  7.     protected $uri = null;
  8.     protected $currentRoute = null;
  9.     protected $params = array();
  10.  
  11.     public function __construct($defaultController = 'index') {
  12.  
  13.         $pattern = '@^/@';
  14.  
  15.         if($_SERVER['PHP_SELF'] !== 'index.php')
  16.             $pattern = '@^.*index.php/@';
  17.  
  18.         $request = preg_replace($pattern, '', $_SERVER['REQUEST_URI'], 1);
  19.  
  20.         $this->uri = explode('/', $request);
  21.  
  22.         if (!isset($this->uri[0]) || trim($this->uri[0]) === '')
  23.             $this->uri[0] = $defaultController;
  24.     }
  25.  
  26.     public function load(array $routes) {
  27.  
  28.         if (isset($routes[$this->uri[0]]))
  29.             $this->currentRoute = $routes[$this->uri[0]];
  30.  
  31.         return $this;
  32.     }
  33.  
  34.     public function route() {
  35.  
  36.         if ($this->currentRoute === null)
  37.             throw new RouterException ('[Router] 404 - Page not found');
  38.  
  39.         $this->parseParams();
  40.         $this->loadGet();
  41.         $this->loadController();
  42.     }
  43.  
  44.     protected function parseParams() {
  45.  
  46.         if (!isset($this->currentRoute['params']))
  47.             return false;
  48.  
  49.         $routeParams = explode('/', $this->currentRoute['params']);
  50.         $uriParams = $this->uri;
  51.  
  52.         array_shift($routeParams);
  53.         array_shift($uriParams);
  54.  
  55.         foreach ($routeParams as $key => $param) {
  56.  
  57.             if (strpos($param, ':') === 0) {
  58.  
  59.                 $param = ltrim($param, ':');
  60.  
  61.                 if (isset($uriParams[$key]) && $this->validParam($param, $uriParams[$key]))
  62.                     $this->params[$param] = $uriParams[$key];
  63.                 else
  64.                     throw new RouterException ('[Router] Missing ' . $param . ' param');
  65.  
  66.             } else {
  67.  
  68.                 $param = ltrim($param, '?');
  69.  
  70.                 if (isset($uriParams[$key]))
  71.                     $this->params[$param] = $uriParams[$key];
  72.                 else
  73.                     $this->params[$param] = '';
  74.             }
  75.         }
  76.     }
  77.  
  78.     protected function validParam($routeParam, $uriValue) {
  79.  
  80.         if (isset($this->currentRoute['valid'][$routeParam])) {
  81.  
  82.             return preg_match('@' . $this->currentRoute['valid'][$routeParam] . '@', $uriValue);
  83.         }
  84.  
  85.         return true;
  86.     }
  87.  
  88.     protected function loadController() {
  89.  
  90.         require_once $this->currentRoute['controller'];
  91.     }
  92.  
  93.     protected function loadGet() {
  94.  
  95.         global $_GET;
  96.  
  97.         $_GET = array_merge($this->params, $_GET);
  98.     }
  99. }
RAW Paste Data