2012-06-25 24 views
32

如何检测CodeIgniter控制器类中的HTTP方法?如何检测CodeIgniter中的HTTP方法

编辑: 是否有任何其他方式比使用笨$_SERVER['REQUEST_METHOD']

+1

可能重复http://stackoverflow.com/questions/359047/php-detecting-request -type-get-post-put-or-delete) – Esailija

+0

我知道,但在CodeIgniter中使用'$ _SERVER'变量是真的吗? –

+0

像你使用任何其他变量 – Esailija

回答

52

感谢布兰登,我已经找到了答案。 $this->input->server($index)$_SERVER[$index]相同。

要获取方法,您可以使用:$this->input->server('REQUEST_METHOD')

UPDATE:(感谢Ecir Hana

随着笨3,采用的method也是可能的:

echo $this->input->method(TRUE); // Outputs: POST 
echo $this->input->method(FALSE); // Outputs: post 
echo $this->input->method(); // Outputs: post 
4

您可以使用输入库检测GET和POST。

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

的更多信息,可以发现:http://ellislab.com/codeigniter%20/user-guide/libraries/input.html

+6

从文档'$ this-> input-> post(); //返回所有没有XSS过滤器的POST项目,所以这并不能真正回答这个问题。因为它获取数据而不是检测HTTP方法。 –

+1

如果(例如发布)请求不包含任何数据,则不起作用。 – Korri

14

在笨3,你可以使用method呃... ...方法的输入类。

从文档:

echo $this->input->method(TRUE); // Outputs: POST 
echo $this->input->method(FALSE); // Outputs: post 
echo $this->input->method(); // Outputs: post 
的[PHP检测请求类型(GET,POST,PUT或DELETE)](