【问题标题】:codeigniter display warning error if argument in URL not provided如果未提供 URL 中的参数,则 codeigniter 显示警告错误
【发布时间】:2011-04-05 14:05:57
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: url codeigniter arguments


    【解决方案1】:

    刚刚知道解决方法很简单:

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

    【讨论】:

      【解决方案2】:

      你也可以使用:

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

      然后做一个重定向,检查:

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

      【讨论】:

        【解决方案3】:

        为你的函数使用默认值

        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 所以它会自动显示错误而无需重定向,如果用户输入网址 www.example.com/profile/id ,

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-24
          • 2016-05-12
          • 2013-05-08
          • 1970-01-01
          • 2021-05-26
          • 1970-01-01
          相关资源
          最近更新 更多