【问题标题】:Conditional URI segment error - Codeigniter条件 URI 段错误 - Codeigniter
【发布时间】:2011-06-25 20:55:32
【问题描述】:

我有一个奇怪的问题,请耐心等待。我正在使用 _remap 函数在我的 URI 中实现 example.com/user/username 协议,并且我正在使用以下代码:

function _remap()
    {       
                //URI Segment I am retrieving (this part works for me)
        $profile_name = $this->uri->segment(2,0);
                // Query the DB where user_name (column in DB) == $profile_name
        $query = $this->db->get_where('users', array('user_name' => $profile_name));
                    // Load user data when URI segment is retrieved, load page
            foreach($query->result() as $row){
                $this->load->view('user_view', $data);          
            }       

}

所以我的问题是,每当我输入一个无效的 URI 段时,即在数据库中找不到它,它只会返回一个空白页。我尝试了一堆条件语句,但基本上我想要这个算法:

if $profile_name = FOUND (in DB)
display page
else 
redirect to error page

就像我说的,我可以让它接受有效的数据库用户名,但如果是无效的,它只会显示一个空白页。我认为这是因为我在 segment(2,0) 函数中包含了 0 参数。让我知道您的想法...非常感谢!

附:以防万一您想知道为什么我不使用路由功能,我不确定是否可以通过路由来完成所有这些工作(无论如何都要对照数据库进行检查)。

【问题讨论】:

    标签: php mysql codeigniter uri codeigniter-url


    【解决方案1】:

    您不需要返回 0,因为如果在该位置没有找到段,URI 类将返回 FALSE(这与返回 0 一样好)

     function _remap()
        {       
                    //URI Segment I am retrieving (this part works for me)
            $profile_name = $this->uri->segment(2);
    
            if(!$profile_name){
                redirect('error_page');
            }
            // Query the DB where user_name (column in DB) == $profile_name
            $query = $this->db->get_where('users', array('user_name' => $profile_name));
            // Load user data when URI segment is retrieved, load page
    
           /* 
            *  Assuming $query returns false if no records are found.  
            *  Or else substitute with another condition
            */
    
            if($query){ 
                foreach($query->result() as $row){
                    $this->load->view('user_view', $data);          
                }
            }else
                 show_error('msg goes here', 404);       
    
    }
    

    现在到您的另一个问题,您可以通过设置自定义路由规则轻松做到这一点,并在您路由到的方法中进行用户数据库检查(因此您可以将 _remap 重命名为实际方法,我们称之为 *fetch_user($username)* 供讨论)

    在您的 routes.php 中,在末尾添加:

    $route['user/(:any)'] = "user/fetch_user";
    

    URI Routing Reference

    您的新 fetch_users 函数:

    function fetch_user($username)
     { 
         // first check if $username has a value or not. We don't want to run a query if this is null.      
        if(!$username)
            redirect('to_some_page')
    
        $query = $this->db->get_where('users', array('user_name' => $username));
    
        /* 
        *  Assuming $query returns false if no records are found.  
        *  Or else substitute with another condition
        */
    
        if($query){ 
           foreach($query->result() as $row){
                 $this->load->view('user_view', $data);          
           }
        }else
          show_error('msg goes here', 404);       
    
    }
    

    【讨论】:

    • 非常感谢,这让我发疯了!另外,我将更深入地研究路由:)。我不确定你是否可以这样做 if($query)!再次感谢!
    • 您可以对您知道可能返回 null/0/false 值的任何内容进行 if($var) 条件检查。这些返回值中的任何一个都将失败 IF 条件。很高兴我能帮忙:)
    【解决方案2】:

    就在你的 foreach 之前,插入这个:

    if (!$query->num_rows()) {
        $this->load->helper('url');
        redirect('error_page_uri_here');
    }
    

    【讨论】:

      猜你喜欢
      • 2013-06-19
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      • 2011-04-10
      相关资源
      最近更新 更多