[英]GET url in Codeigniter
我正在為我的項目使用codeigniter。 要獲得uri細分,我知道我可以使用
$this->uri->segment();
但我的情況有點不同
我的網址看起來像
localhost/mediabox/home/box/21
但是,一旦我轉到這個URL,就會出現一個彈出形式,用戶提供一個訪問此頁面的密鑰,我使用ajax方法驗證密鑰,該方法位於我的家庭控制器validate_key函數中
當我回顯網址時,它會給我localhost / home / validate_key
在調用home控制器的valiate_key時,如何從url欄中的url wrritten獲取21?
有任何想法嗎?
謝謝
這不是一個錯誤,這是一種自然的行為。
考慮以下:
您可以通過在地址欄中鍵入URL來從服務器請求validate_key
函數。 current_url()
返回localhost/blabla/validate_key
。 沒有涉及AJAX。
使用AJAX請求validate_key
。 將執行相同的PHP代碼。
current_url()
將更改為localhost/blabla/validate_key
即使您的瀏覽器的地址欄顯示localhost/blabla/box/21
。
那么,這意味着什么? 這意味着Codeigniter base_url()
不關心你的地址欄,它關心它所處的功能,它是通過ajax
還是普通請求調用的。
所以只要這個函數被執行, URL就指向它 。
我最喜歡這種情況的解決方案是簡單地創建一個隱藏的輸入。
簡單地說,當用戶請求box
功能時。 你正在向他展示一個彈出窗體。 所以添加一個hidden_input
字段,給它一個名字,值為21(取決於)。
例如(您應該根據您的特定需求定制):
將其添加到由box
函數顯示的視圖中的表單中:
form_hidden("number", $this->uri->segment(3));
;
現在這些數據將被發送到您的validate_key
函數。 我們如何訪問它? 這很簡單!
function validate_key(){
$this->input->post("number");//returns 21 or whatever in the URL.
//OR if the form sends GET request
$this->input->get("number");//return 21 or whatever in the URL.
/*
*Or , you can do the following it's considered much safer when you're ONLY
*expecting numbers, since this function(intval) will get the integer value of
*the uri segment which might be a destructive string, so if it's a string
*this function will simply return 0.
*/
$number = intval($this->input->post("number"));//returns 21 or whatever in the URL.
//Or if it it GET request:
$number = intval($this->input->get("number"));//returns 21 or whatever in the URL.
}
看起來你已經使用.htaccess刪除了url的index.php
部分。 因此,當您導航到localhost/mediabox/home/box/21
您將值21傳遞給名為home
的控制器中名為box
的函數
如果你想在validate_key
函數中保留該值,只需在調用它時傳遞它:
function box($param)
{
//$param = 21
$this->validate_key($param);
}
它更好,並且建議使用隱藏字段並在需要時發布值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.