2012-06-25 27 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)](