如果未提供URL中的参数,则codeigniter显示警告错误

时间:2010-08-30 03:11:19

标签: url codeigniter arguments

如果我没有在Codeigniter的URL中提供值,它将显示PHP错误,说它缺少函数调用的参数。我根本不希望它显示出来。如果URL中没有参数,则应该重新路由到其索引函数。我该如何防止这种情况发生?

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

任何人都会猜测并输入“www.example.com/profile/id”

会产生错误。我该如何预防呢?我试过,“如果(!isset($ id))”但它在达到该条件之前产生了错误。

3 个答案:

答案 0 :(得分:5)

刚刚得知解决方案很简单:

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

}

答案 1 :(得分:3)

你也可以使用:

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

然后进行重定向,请查看:

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

答案 2 :(得分: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();
 }

}

如果用户输入网址www.example.com/profile/id,

,就不会有id = 0所以它自动淋浴没有重定向