Created
February 6, 2020 04:40
-
-
Save gaibz/7e02dffe50dc6b5b8fac1d1c6b449b6e to your computer and use it in GitHub Desktop.
CodeIgniter 3 request Helper [GET | POST | REQUEST | SERVER]
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 | |
/** | |
* I made this file just for make things easier | |
* | |
* Sometimes I just dont like how CI Input Class works. $this->input->[method] is just too long for me | |
* So, with this helper it could be simple just see @example | |
* And this helper also have function for output JSON .. | |
* | |
* @example $username = request("username","POST"); | |
* // it will empty if the request variable is not set | |
* @author Herlangga Sefani Wijaya | 6 Feb 2020 | |
*/ | |
/** | |
* get HTTP request from $_POST, $_GET, $_REQUEST, $_SERVER | |
* | |
* @param String $req_var = Request Variable | |
* @param String $req_method = Request Method (POST/GET/REQUEST/SERVER) default AUTO | |
* @return String empty string if no Request Variable registered | |
*/ | |
function request(String $req_var, String $req_method="AUTO"){ | |
$ret = ""; | |
if(strtoupper($req_method) === "POST"){ | |
$ret = empty($_POST[$req_var]) ? "" : $_POST[$req_var]; | |
} | |
else if(strtoupper($req_method) === "GET"){ | |
$ret = empty($_GET[$req_var]) ? "" : $_GET[$req_var]; | |
} | |
else if(strtoupper($req_method) === "REQUEST"){ | |
$ret = empty($_REQUEST[$req_var]) ? "" : $_REQUEST[$req_var]; | |
} | |
else if(strtoupper($req_method) === "SERVER"){ | |
$ret = empty($_SERVER[$req_var]) ? "" : $_SERVER[$req_var]; | |
} | |
else if(strtoupper($req_method) === "AUTO"){ | |
if(!empty($_POST[$req_var])){ | |
$ret = $_POST[$req_var]; | |
} | |
else if(!empty($_GET[$req_var])){ | |
$ret = $_GET[$req_var]; | |
} | |
else if(!empty($_REQUEST[$req_var])){ | |
$ret = $_REQUEST[$req_var]; | |
} | |
else if(!empty($_SERVER[$req_var])){ | |
$ret = $_SERVER[$req_var]; | |
} | |
} | |
return $ret; | |
} | |
/** | |
* Handle Output for JSON | |
* | |
* @param Array $output = Output array for printed as JSON | |
*/ | |
function output_json(Array $output=[]){ | |
$CI =& get_instance(); | |
if(ENVIRONMENT === "development"){ | |
$output["request"] = $_REQUEST; | |
} | |
$CI->output->set_content_type("application/json")->set_output(json_encode($output)); | |
} | |
/* End of file application/helpers/request_helper.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment