request.php
上传用户:shuoshiled
上传日期:2018-01-28
资源大小:10124k
文件大小:3k
源码类别:

中间件编程

开发平台:

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.             $this->id = (isset($params['id'])) ? $params['id'] : null;
  26.             $this->params = (isset($params['data'])) ? json_decode(stripslashes($params['data']), true) : null;
  27.         } else {
  28.             // grab JSON data if there...
  29.             $this->params = (isset($_REQUEST['data'])) ? json_decode(stripslashes($_REQUEST['data']), true) : null;
  30.             $this->id = (isset($_REQUEST['id'])) ? json_decode(stripslashes($_REQUEST['id']), true) : null;
  31.         }
  32.         // Quickndirty PATH_INFO parser
  33.         if (isset($_SERVER["PATH_INFO"])){
  34.             $cai = '/^/([a-z]+w)/([a-z]+w)/([0-9]+)$/';  // /controller/action/id
  35.             $ca =  '/^/([a-z]+w)/([a-z]+)$/';              // /controller/action
  36.             $ci = '/^/([a-z]+w)/([0-9]+)$/';               // /controller/id
  37.             $c =  '/^/([a-z]+w)$/';                             // /controller
  38.             $i =  '/^/([0-9]+)$/';                             // /id
  39.             $matches = array();
  40.             if (preg_match($cai, $_SERVER["PATH_INFO"], $matches)) {
  41.                 $this->controller = $matches[1];
  42.                 $this->action = $matches[2];
  43.                 $this->id = $matches[3];
  44.             } else if (preg_match($ca, $_SERVER["PATH_INFO"], $matches)) {
  45.                 $this->controller = $matches[1];
  46.                 $this->action = $matches[2];
  47.             } else if (preg_match($ci, $_SERVER["PATH_INFO"], $matches)) {
  48.                 $this->controller = $matches[1];
  49.                 $this->id = $matches[2];
  50.             } else if (preg_match($c, $_SERVER["PATH_INFO"], $matches)) {
  51.                 $this->controller = $matches[1];
  52.             } else if (preg_match($i, $_SERVER["PATH_INFO"], $matches)) {
  53.                 $this->id = $matches[1];
  54.             }
  55.         }
  56.     }
  57. }