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.     public function to_string() {
  16.         return "controller: " . $this->controller . ', action: ' . $this->action . ', id: ' . $this->id;
  17.     }
  18.     protected function parseRequest() {
  19.         if ($this->method == 'PUT') {   // <-- Have to jump through hoops to get PUT data
  20.             $raw  = '';
  21.             $httpContent = fopen('php://input', 'r');
  22.             while ($kb = fread($httpContent, 1024)) {
  23.                 $raw .= $kb;
  24.             }
  25.             fclose($httpContent);
  26.             $params = array();
  27.             parse_str($raw, $params);
  28.             $this->id = (isset($params['id'])) ? $params['id'] : null;
  29.             $this->params = (isset($params['data'])) ? json_decode(stripslashes($params['data']), true) : null;
  30.         } else {
  31.             // grab JSON data if there...
  32.             $this->params = (isset($_REQUEST['data'])) ? json_decode(stripslashes($_REQUEST['data']), true) : null;
  33.             $this->id = (isset($_REQUEST['id'])) ? json_decode(stripslashes($_REQUEST['id']), true) : null;
  34.         }
  35.         // parse path info
  36.         if (isset($_SERVER["PATH_INFO"])){
  37.             $cai = '/^/([a-z]+w)/([a-z]+w)/([0-9]+)$/';    // /controller/action/id
  38.             $ca =  '/^/([a-z]+w)/([a-z]+w)$/';              // /controller/action
  39.             $ci = '/^/([a-z]+w)/([0-9]+)$/';               // /controller/id
  40.             $c =  '/^/([a-z]+)$/';                             // /controller
  41.             $i =  '/^/([0-9]+)$/';                             // /id
  42.             $matches = array();
  43.             if (preg_match($cai, $_SERVER["PATH_INFO"], $matches)) {
  44.                 $this->controller = $matches[1];
  45.                 $this->action = $matches[2];
  46.                 $this->id = $matches[3];
  47.             } else if (preg_match($ca, $_SERVER["PATH_INFO"], $matches)) {
  48.                 $this->controller = $matches[1];
  49.                 $this->action = $matches[2];
  50.             } else if (preg_match($ci, $_SERVER["PATH_INFO"], $matches)) {
  51.                 $this->controller = $matches[1];
  52.                 $this->id = $matches[2];
  53.             } else if (preg_match($c, $_SERVER["PATH_INFO"], $matches)) {
  54.                 $this->controller = $matches[1];
  55.             } else if (preg_match($i, $_SERVER["PATH_INFO"], $matches)) {
  56.                 $this->id = $matches[1];
  57.             }
  58.         }
  59.     }
  60. }