Check whether a request is GET or POST
Better use $_SERVER['REQUEST_METHOD']
:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// …
}
Check if request is GET or POST
I've solve my problem like below in laravel version: 7+
In routes/web.php:
Route::post('url', YourController@yourMethod);
In app/Http/Controllers:
public function yourMethod(Request $request) {
switch ($request->method()) {
case 'POST':
// do anything in 'post request';
break;
case 'GET':
// do anything in 'get request';
break;
default:
// invalid request
break;
}
}
Check if request is Get AND Post
A request cannot be Both POST and GET at the same time, i think i understand your question.
To get the querystring values on a POST request you can do:
$queryString = $_SERVER['QUERY_STRING'];
$queryStringAsDictionary = parse_str($queryString);
And get the Post values with $_POST
How can I check if request was a POST or GET request in codeigniter?
I've never used codeigniter, but for this I check the $_SERVER['REQUEST_METHOD']
.
Looking at the docs maybe something like:
if ($this->input->server('REQUEST_METHOD') === 'GET') {
//its a get
} elseif ($this->input->server('REQUEST_METHOD') === 'POST') {
//its a post
}
If you're going to use it a lot then it's simple to roll your own isGet()
function for it.
How can I check if request was a POST or GET request in Symfony2 or Symfony3
If you want to do it in controller,
$this->getRequest()->isMethod('GET');
or in your model (service), inject or pass the Request object to your model first, then do the same like the above.
Edit: for Symfony 3 use this code
if ($request->isMethod('post')) {
// your code
}
Related Topics
Dependency Hell - How Does One Pass Dependencies to Deeply Nested Objects
Laravel 5.4 on PHP 7.0: Pdo Exception - Could Not Find Driver (Mysql)
How to Read Large Worksheets from Large Excel Files (27Mb+) with PHPexcel
$_Server["Remote_Addr"] Gives Server Ip Rather Than Visitor Ip
How to Check If a Url Exists in PHP
PHP Move_Uploaded_File() Error
Simplest Way to Increment a Date in PHP
PHP File Listing Multiple File Extensions
Can't Send Email to Addresses at My Own Domain
How to Check If a Row Exist in the Database Using Pdo
Laravel: How to Get Current Route Name? (V5 ... V7)
Codeigniter - How to Catch Db Errors