Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect HTTP method in CodeIgniter

How can I detect HTTP method in CodeIgniter controller class?

Edited: Is there any other way than using $_SERVER['REQUEST_METHOD'] in CodeIgniter?

like image 683
Sgn. Avatar asked Jun 25 '12 13:06

Sgn.


People also ask

How to check request method in codeigniter?

Determining Request Type This can be checked with the isAJAX() and isCLI() methods: <? php // Check for AJAX request. if ($request->isAJAX()) { // ... } // Check for CLI Request if ($request->isCLI()) { // ... }

What is request in codeigniter?

The request class is an object-oriented representation of an HTTP request. This is meant to work for both incoming, such as a request to the application from a browser, and outgoing requests, like would be used to send a request from the application to a third-party application.


2 Answers

Thanks to Branden, I've found the answer. $this->input->server($index) is identical to $_SERVER[$index].

To get method you can use: $this->input->server('REQUEST_METHOD').

UPDATE: (thanks to Ecir Hana)

As of CodeIgniter 3, using of method is also possible:

echo $this->input->method(TRUE); // Outputs: POST echo $this->input->method(FALSE); // Outputs: post echo $this->input->method(); // Outputs: post 
like image 155
Sgn. Avatar answered Sep 27 '22 21:09

Sgn.


In CodeIgniter 3, you can use the method uhm...method of Input Class.

From the docs:

echo $this->input->method(TRUE); // Outputs: POST echo $this->input->method(FALSE); // Outputs: post echo $this->input->method(); // Outputs: post 
like image 39
Ecir Hana Avatar answered Sep 27 '22 23:09

Ecir Hana