35

How can I detect HTTP method in CodeIgniter controller class?

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

Sgn.
  • 1,696
  • 2
  • 14
  • 27

3 Answers3

64

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
Community
  • 1
  • 1
Sgn.
  • 1,696
  • 2
  • 14
  • 27
19

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
Ecir Hana
  • 10,864
  • 13
  • 67
  • 117
5

You can detect GET and POST by using the Input library.

$this->input->post() or $this->input->get()

More information can be found: http://ellislab.com/codeigniter%20/user-guide/libraries/input.html

jason
  • 1,132
  • 14
  • 32
Branden Martin
  • 374
  • 2
  • 10
  • 6
    From the docs `$this->input->post(); // returns all POST items without XSS filter` so this does not really answer the question. As it gets data instead of detecting the HTTP method. – Thomas Welton May 28 '13 at 15:10
  • 3
    Doesn't work if the (post for example) request doesn't contain any data. – Korri Jan 08 '15 at 22:39