Skip to content

Commit df11a54

Browse files
author
Phil Sturgeon
committed
Merge pull request chriskacerguis#131 from kwoodfriend/master
Moved input parsing to separate methods and support for alternative HTTP methods
2 parents 407b253 + 420c1ff commit df11a54

File tree

1 file changed

+60
-39
lines changed

1 file changed

+60
-39
lines changed

application/libraries/REST_Controller.php

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ abstract class REST_Controller extends CI_Controller
3131
*/
3232
protected $methods = array();
3333

34+
/**
35+
* List of allowed HTTP methods
36+
*
37+
* @var array
38+
*/
39+
protected $allowed_http_methods = array('get', 'delete', 'post', 'put');
40+
3441
/**
3542
* General request data and information.
3643
* Stores accept, language, body, headers, etc.
@@ -156,42 +163,7 @@ public function __construct()
156163
// Some Methods cant have a body
157164
$this->request->body = NULL;
158165

159-
switch ($this->request->method)
160-
{
161-
case 'get':
162-
// Grab proper GET variables
163-
parse_str(parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY), $get);
164-
165-
// Merge both the URI segments and GET params
166-
$this->_get_args = array_merge($this->_get_args, $get);
167-
break;
168-
169-
case 'post':
170-
$this->_post_args = $_POST;
171-
172-
$this->request->format and $this->request->body = file_get_contents('php://input');
173-
break;
174-
175-
case 'put':
176-
// It might be a HTTP body
177-
if ($this->request->format)
178-
{
179-
$this->request->body = file_get_contents('php://input');
180-
}
181-
182-
// If no file type is provided, this is probably just arguments
183-
else
184-
{
185-
parse_str(file_get_contents('php://input'), $this->_put_args);
186-
}
187-
188-
break;
189-
190-
case 'delete':
191-
// Set up out DELETE variables (which shouldn't really exist, but sssh!)
192-
parse_str(file_get_contents('php://input'), $this->_delete_args);
193-
break;
194-
}
166+
$this->{'_parse_' . $this->request->method}();
195167

196168
// Now we know all about our request, let's try and parse the body if it exists
197169
if ($this->request->format and $this->request->body)
@@ -355,7 +327,7 @@ public function response($data = array(), $http_code = null)
355327
{
356328
$http_code = 404;
357329

358-
//create the output variable here in the case of $this->response(array());
330+
// create the output variable here in the case of $this->response(array());
359331
$output = NULL;
360332
}
361333

@@ -527,7 +499,7 @@ protected function _detect_output_format()
527499
/**
528500
* Detect method
529501
*
530-
* Detect which method (POST, PUT, GET, DELETE) is being used
502+
* Detect which HTTP method is being used
531503
*
532504
* @return string
533505
*/
@@ -547,7 +519,7 @@ protected function _detect_method()
547519
}
548520
}
549521

550-
if (in_array($method, array('get', 'delete', 'post', 'put')))
522+
if (in_array($method, $this->allowed_http_methods) && method_exists($this, '_parse_' . $method))
551523
{
552524
return $method;
553525
}
@@ -768,6 +740,55 @@ protected function _auth_override_check()
768740
return false;
769741
}
770742

743+
/**
744+
* Parse GET
745+
*/
746+
protected function _parse_get()
747+
{
748+
// Grab proper GET variables
749+
parse_str(parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY), $get);
750+
751+
// Merge both the URI segments and GET params
752+
$this->_get_args = array_merge($this->_get_args, $get);
753+
}
754+
755+
/**
756+
* Parse POST
757+
*/
758+
protected function _parse_post()
759+
{
760+
$this->_post_args = $_POST;
761+
762+
$this->request->format and $this->request->body = file_get_contents('php://input');
763+
}
764+
765+
/**
766+
* Parse PUT
767+
*/
768+
protected function _parse_put()
769+
{
770+
// It might be a HTTP body
771+
if ($this->request->format)
772+
{
773+
$this->request->body = file_get_contents('php://input');
774+
}
775+
776+
// If no file type is provided, this is probably just arguments
777+
else
778+
{
779+
parse_str(file_get_contents('php://input'), $this->_put_args);
780+
}
781+
}
782+
783+
/**
784+
* Parse DELETE
785+
*/
786+
protected function _parse_delete()
787+
{
788+
// Set up out DELETE variables (which shouldn't really exist, but sssh!)
789+
parse_str(file_get_contents('php://input'), $this->_delete_args);
790+
}
791+
771792
// INPUT FUNCTION --------------------------------------------------------------
772793

773794
/**

0 commit comments

Comments
 (0)