Ejemplo n.º 1
0
 public function __construct()
 {
     // Get the current request method.
     $this->method = strtoupper($_SERVER['REQUEST_METHOD']);
     // Get the requested URI.
     $this->uri = $_SERVER['REQUEST_URI'];
     // Check if the type of the request is allowed.
     // If it's not - exit with error code 405.
     if (!in_array($this->method, $this->allowedMethods)) {
         return Response::methodNotAllowed($this->method, $this->uri);
     } else {
         // Check if X-HTTP-Method header is added to the POST request
         // and check for validity (either a DELETE or a PUT request).
         if ($this->method === 'POST' && array_key_exists('HTTP_X_HTTP_METHOD', $_SERVER)) {
             if ($_SERVER['HTTP_X_HTTP_METHOD'] === 'DELETE') {
                 $this->method = 'DELETE';
             } else {
                 if ($_SERVER['HTTP_X_HTTP_METHOD'] === 'PUT') {
                     $this->method = 'PUT';
                 } else {
                     return Response::methodNotAllowed($this->method, $this->uri);
                 }
             }
         }
     }
     // If it's a put request then add the input add to the arguments.
     if ($this->method === 'PUT') {
         $input = json_decode(file_get_contents('php://input'));
         foreach ($input as $key => $value) {
             $this->arguments[$key] = $value;
         }
     }
     $request = explode('/', $this->uri);
     $this->controller = count($request) > 1 ? ucwords($request[1] . 'Controller') : null;
     if (count($request) > 2 && $request[2] !== null) {
         if (is_numeric($request[2])) {
             $this->action = null;
             $this->arguments['id'] = $request[2];
         } else {
             $this->action = strtolower($request[2]);
         }
     }
     if (count($request) > 3) {
         $this->arguments['id'] = $request[3];
     }
 }