Check Whether a Request Is Get or Post

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



Leave a reply



Submit