Erlendftw

flexRouter Legacy v1.11

Aug 16th, 2017
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.43 KB | None | 0 0
  1. <?php
  2. /**
  3.  * flexRouter - a small, lightweight php-router for clean, restful urls. Extremely easy to setup and configure.
  4.  * Supports setting up routes based on their mode (GET, POST). E.x: GET /user/:id/profile or POST /user/:id:/update-profile
  5.  * Supports wildcards aswell. E.g. /picture/:id/edit*
  6.  * @author Erlend Ellingsen <[email protected]>
  7.  * @version 1.11 03.08.2017
  8.  */
  9. class flexRouter {
  10.     public $basePath = '';
  11.     public $routed = false;
  12.     public $caseSensitive = true;
  13.     private $pathCt = 0;
  14.     private $mode;
  15.     private $params;
  16.     private $dynParams;
  17.     public function __construct($caseSensitive = true) {
  18.         $this->caseSensitive = $caseSensitive;
  19.         // Determine the path
  20.         if (!isset($_GET['path'])) $_GET['path'] = '';
  21.         $this->params = explode('/', $_GET['path']);
  22.         if (!$this->caseSensitive) {
  23.             for ($i = 0; $i < count($this->params); $i++) { $this->params[$i] = strtolower($this->params[$i]); }
  24.         }
  25.         $this->pathCt = count($this->params);
  26.         for ($i = 0; $i < $this->pathCt - 1; $i++) {
  27.             $this->basePath .= '../';
  28.         }
  29.         // Determine the mode
  30.         $this->mode = (empty($_POST) ? 'GET' : 'POST');
  31.     }
  32.     public function getMode() {
  33.         return $this->mode;
  34.     }
  35.     public function resetRouted() {
  36.         $this->routed = false;
  37.     }
  38.     /**
  39.      * Mode: GET/POST
  40.      * Pattern: /users/:id/update
  41.      */
  42.     public function route($mode, $pattern) {
  43.         $this->dynParams = array();
  44.         // Validate mode
  45.         if (is_array($mode)) {
  46.             $modeValid = false;
  47.             for ($i = 0; $i < count($mode); $i++) {
  48.                 $cM = $mode[$i];
  49.                 if (strtolower($cM) == strtolower($this->mode)) $modeValid = true;
  50.                 if ($modeValid) break;
  51.             }
  52.             if (!$modeValid) return false;
  53.         } else if ($mode != '*') {
  54.             if (strtolower($mode) != strtolower($this->mode)) return false;
  55.         }
  56.         // Check empty pattern
  57.         if ($pattern == '' && ($this->pathCt == 0 || ($this->pathCt == 1 && $this->params[0] == ''))) {
  58.             $this->routed = true;
  59.             return true;
  60.         }
  61.         // Validate pattern
  62.         $wildCard = (substr($pattern, -1) == "*" ? true : false);
  63.         if ($wildCard) $pattern = substr($pattern, 0, strlen($pattern) - 1);
  64.         $routePattern = explode('/', $pattern);
  65.         if (empty($routePattern[0])) unset($routePattern[0]);
  66.         $routePattern = array_values($routePattern);
  67.         $routePatternCt = count($routePattern);
  68.         $validWildCard = ($wildCard && $this->pathCt >= $routePatternCt);
  69.         if (!$validWildCard && ($routePatternCt != $this->pathCt)) return false;
  70.         for ($i = 0; $i < $routePatternCt; $i++) {
  71.             $patternElement = $routePattern[$i];
  72.             if ($patternElement[0] == ':') {
  73.                 $this->dynParams[$patternElement] = $this->params[$i];
  74.                 continue;
  75.             }
  76.             if ($this->caseSensitive && ($patternElement != $this->params[$i])) return false;
  77.             if (!$this->caseSensitive && (strtolower($patternElement) != $this->params[$i])) return false;
  78.             // end of routePattern-loop
  79.         }
  80.         $this->routed = true;
  81.         return true;
  82.         // end route-function
  83.     }
  84.     public function param($tag) {
  85.         return $this->dynParams[$tag];
  86.     }
  87.     // end flexRouter
  88. }
  89. ?>
Advertisement
Add Comment
Please, Sign In to add comment