Guest User

Untitled

a guest
Jul 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.91 KB | None | 0 0
  1. <?php
  2.  
  3. class Request
  4. {
  5.     var $path = '';
  6.     var $queryString = '';
  7.    
  8.     var $params = array('module' => '', 'p' => '', 'page' => '');
  9.     public function __construct()
  10.     {
  11.         $request_uri = $this->getUrl();
  12.    
  13.         $__tmpUri = parse_url($request_uri);
  14.         $this->path = isset($__tmpUri['path']) ? $__tmpUri['path'] : '';
  15.         $this->queryString = isset($__tmpUri['query']) ? $__tmpUri['query'] : '';
  16.     }
  17.    
  18.     public function getParams()
  19.     {
  20.         if(!empty($this->queryString))
  21.         {
  22.              parse_str($this->queryString, $array2String);
  23.              foreach ($array2String as $k=>$v)
  24.              {
  25.                 $this->params[$k] = $v;
  26.              }
  27.  
  28.         }
  29.         if(!empty($this->path))
  30.         {
  31.             $request_params = explode("/", $this->path);
  32.         }
  33.         else
  34.         {
  35.             $request_params = array();
  36.         }
  37.  
  38.         $cnt = 1;
  39.         if(count($request_params))
  40.         {
  41.             foreach ($request_params as $k => $v)
  42.             {
  43.                
  44.                 if(!empty($v))
  45.                 {
  46.                     if(empty($this->params['module']))
  47.                     {
  48.                         if(is_numeric($v))
  49.                         {
  50.                             $this->params['page'] = $v;
  51.                         }
  52.                         else
  53.                         {
  54.                             $this->params['module'] = $v;
  55.                         }
  56.                         $cnt++;
  57.                         continue;
  58.                     }
  59.                    
  60.                    
  61.                     if(empty($this->params['p']))
  62.                     {
  63.                         if(is_numeric($v))
  64.                         {
  65.                             $this->params['page'] = $v;
  66.                         }
  67.                         else
  68.                         {
  69.                             $this->params['p'] = $v;
  70.                         }
  71.                         $cnt++;
  72.                         continue;
  73.                     }
  74.                    
  75.                     if($k == $cnt)
  76.                     {
  77.                         $this->params[$v] = $request_params[$k + 1];
  78.                         $cnt+=2;
  79.                         continue;
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.         return $this->params;
  85.    
  86.     }
  87.    
  88.     public function getParam($param, $defaultValue = null)
  89.     {
  90.         $this->getParams();
  91.        
  92.         if(isset($this->params[$param]))
  93.         {
  94.             return $this->params[$param];
  95.         }
  96.         else
  97.         {
  98.             return $defaultValue;
  99.         }
  100.     }
  101.    
  102.     protected function getUrl()
  103.     {
  104.         $request_uri = $_SERVER['REQUEST_URI'];
  105.         if($request_uri{0} != '/')
  106.         {
  107.             $request_uri = "/".$request_uri;
  108.         }
  109.         return  $request_uri;
  110.     }
  111. }
  112.  
  113. ?>
Add Comment
Please, Sign In to add comment