request.php
上传用户:dawnssy
上传日期:2022-08-06
资源大小:9345k
文件大小:3k
源码类别:

JavaScript

开发平台:

JavaScript

  1. <?php
  2. /**
  3.  * @class Request
  4.  */
  5. class Request {
  6.     public $restful, $method, $controller, $action, $id, $params;
  7.     public function __construct($params) {
  8.         $this->restful = (isset($params["restful"])) ? $params["restful"] : false;
  9.         $this->method = $_SERVER["REQUEST_METHOD"];
  10.         $this->parseRequest();
  11.     }
  12.     public function isRestful() {
  13.         return $this->restful;
  14.     }
  15.     protected function parseRequest() {
  16.         if ($this->method == 'PUT') {   // <-- Have to jump through hoops to get PUT data
  17.             $raw  = '';
  18.             $httpContent = fopen('php://input', 'r');
  19.             while ($kb = fread($httpContent, 1024)) {
  20.                 $raw .= $kb;
  21.             }
  22.             fclose($httpContent);
  23.             $params = array();
  24.             parse_str($raw, $params);
  25.             if (isset($params['data'])) {
  26.                 $this->params =  json_decode(stripslashes($params['data']));
  27.             } else {
  28.                 $params = json_decode(stripslashes($raw));
  29.                 $this->params = $params->data;
  30.             }
  31.         } else {
  32.             // grab JSON data if there...
  33.             $this->params = (isset($_REQUEST['data'])) ? json_decode(stripslashes($_REQUEST['data'])) : null;
  34.             if (isset($_REQUEST['data'])) {
  35.                 $this->params =  json_decode(stripslashes($_REQUEST['data']));
  36.             } else {
  37.                 $raw  = '';
  38.                 $httpContent = fopen('php://input', 'r');
  39.                 while ($kb = fread($httpContent, 1024)) {
  40.                     $raw .= $kb;
  41.                 }
  42.                 $params = json_decode(stripslashes($raw));
  43.                 $this->params = $params->data;
  44.             }
  45.         }
  46.         // Quickndirty PATH_INFO parser
  47.         if (isset($_SERVER["PATH_INFO"])){
  48.             $cai = '/^/([a-z]+w)/([a-z]+w)/([0-9]+)$/';  // /controller/action/id
  49.             $ca =  '/^/([a-z]+w)/([a-z]+)$/';              // /controller/action
  50.             $ci = '/^/([a-z]+w)/([0-9]+)$/';               // /controller/id
  51.             $c =  '/^/([a-z]+w)$/';                             // /controller
  52.             $i =  '/^/([0-9]+)$/';                             // /id
  53.             $matches = array();
  54.             if (preg_match($cai, $_SERVER["PATH_INFO"], $matches)) {
  55.                 $this->controller = $matches[1];
  56.                 $this->action = $matches[2];
  57.                 $this->id = $matches[3];
  58.             } else if (preg_match($ca, $_SERVER["PATH_INFO"], $matches)) {
  59.                 $this->controller = $matches[1];
  60.                 $this->action = $matches[2];
  61.             } else if (preg_match($ci, $_SERVER["PATH_INFO"], $matches)) {
  62.                 $this->controller = $matches[1];
  63.                 $this->id = $matches[2];
  64.             } else if (preg_match($c, $_SERVER["PATH_INFO"], $matches)) {
  65.                 $this->controller = $matches[1];
  66.             } else if (preg_match($i, $_SERVER["PATH_INFO"], $matches)) {
  67.                 $this->id = $matches[1];
  68.             }
  69.         }
  70.     }
  71. }