2010-08-30 55 views
3

如果我沒有在Codeigniter的URL中提供值,則會顯示遇到的PHP錯誤,表示它是函數調用的缺少參數。我不希望它顯示。如果URL中沒有參數,它應該重新路由到它的索引函數。我如何防止這種情況發生?如果URL中的參數未提供,codeigniter將顯示警告錯誤

例如,www.example.com/profile/id/45

有人猜想像這樣 「www.example.com/profile/id」

會產生錯誤輸入。我如何防止這種情況發生?我試過,「if(!isset($ id))」,但它在達到該條件之前產生錯誤。

回答

5

剛剛獲悉,解決方法很簡單:

function id($id=NULL) 
{ 
if ($id===NULL) 
{ 
    redirect.... 
} 

} 
3

你也可以使用:

$this->uri->segment(2); // gets 'id' 
$this->uri->segment(3); // gets '45' 

然後做一個重定向,請上網:

http://codeigniter.com/user_guide/helpers/url_helper.html 
0

使用默認值你的功能

function id($id=0){ 
    if(there any record with $id being provided){ 
     //do something 
}else{ 
     //id not provided,set to default values 0 and then 
    show_404(); 
} 

} 

沒有辦法可以有id = 0所以它自動showerror沒有重定向,如果用戶輸入的網址www.example.com/profile/id,

相關問題