Last active
December 20, 2015 23:38
-
-
Save chill117/6213391 to your computer and use it in GitHub Desktop.
Adds support for PUT verb in CodeIgniter; populates PHP's $_POST data super global with data for PUT requests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); | |
/* | |
Description: | |
Adds support for PUT verb in CodeIgniter; populates PHP's $_POST data | |
super global with data for PUT requests. | |
How to Use: | |
Add the following to your 'application/config/hooks.php' file: | |
$hook['pre_controller'][] = array( | |
'class' => 'ExtendHttpSupport', | |
'function' => 'pre_controller', | |
'filename' => 'ExtendHttpSupport.php', | |
'filepath' => 'hooks', | |
'params' => array() | |
); | |
Also, make sure hooks are enabled in your 'application/config/config.php' file. | |
*/ | |
class ExtendHttpSupport | |
{ | |
/* | |
Fills the $_POST super global with the Raw Post Data sent to the server. | |
See: | |
http://php.net/manual/en/reserved.variables.httprawpostdata.php | |
*/ | |
public function pre_controller() | |
{ | |
$this->parse_inputs(); | |
} | |
protected function parse_inputs() | |
{ | |
// Only continue if this is a POST or PUT request. | |
if ( | |
!$this->is_post_request() && | |
!$this->is_put_request() | |
) | |
return; | |
$raw_post_data = file_get_contents('php://input'); | |
$parsed_data = $this->parse_data($raw_post_data); | |
if ($parsed_data) | |
$_POST = $parsed_data; | |
} | |
protected function parse_data($data) | |
{ | |
$content_type = $_SERVER['CONTENT_TYPE']; | |
if (strpos($_SERVER['CONTENT_TYPE'], ';') !== false) | |
list ($content_type) = explode(';', $_SERVER['CONTENT_TYPE']); | |
switch ($content_type) | |
{ | |
case 'application/json': | |
return $this->parse_data__json($data); | |
break; | |
} | |
return $this->parse_data__urlencoded($data); | |
} | |
protected function parse_data__urlencoded($data) | |
{ | |
if (!(strlen($data) > 0)) | |
return array(); | |
$parsed = array(); | |
foreach (explode('&', $data) as $key_value_pair) | |
{ | |
if (strpos($key_value_pair, '=') !== false) | |
{ | |
list ($key, $value) = explode('=', $key_value_pair); | |
} | |
else | |
{ | |
$key = $key_value_pair; | |
$value = ''; | |
} | |
$parsed[urldecode($key)] = urldecode($value); | |
} | |
return $parsed; | |
} | |
protected function parse_data__json($data) | |
{ | |
return json_decode($data, true); | |
} | |
protected function is_post_request() | |
{ | |
return strtoupper($_SERVER['REQUEST_METHOD']) === 'POST'; | |
} | |
protected function is_put_request() | |
{ | |
return strtoupper($_SERVER['REQUEST_METHOD']) === 'PUT'; | |
} | |
} | |
/* End of file ExtendHttpSupport.php */ | |
/* Location: ./application/hooks/ExtendHttpSupport.php */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); | |
/* | |
Add the hooks found here to your application/config/hooks.php file. | |
And, don't forget to enable hooks ($config['enable_hooks']) in your application/config/config.php file. | |
*/ | |
$hook['pre_controller'][] = array( | |
'class' => 'ExtendHttpSupport', | |
'function' => 'pre_controller', | |
'filename' => 'ExtendHttpSupport.php', | |
'filepath' => 'hooks', | |
'params' => array() | |
); | |
/* End of file hooks.php */ | |
/* Location: ./application/config/hooks.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment