【问题标题】:CodeIgniter: current_url shows question markCodeIgniter:current_url 显示问号
【发布时间】:2012-05-07 02:35:17
【问题描述】:

假设我的页面已打开:

http://localhost/app1/profile/index/123/

current_url() 的结果是这样的:

http://localhost/app1/?profile/index/123

有一个不应该存在的?。问号似乎是由以下配置设置引起的:

$config['enable_query_strings'] = TRUE;

我需要在我的应用程序中启用查询字符串。有什么想法我需要做什么?

编辑 1:

另外,如果 URL 确实有查询字符串,我需要 current_url 也返回它。我希望 Phil Sturgeon 的解决方案 CodeIgniter current_url doesn't show query strings 能帮助我。

我正在使用 CI 2.1.0。

【问题讨论】:

    标签: codeigniter codeigniter-2 codeigniter-url


    【解决方案1】:

    如前所述,$config['enable_query_strings'] 是 Codeigniter 中的一种“遗留”设置,当时没有支持 $_GET(真的,没有)。

    http://codeigniter.com/user_guide/general/urls.html

    启用查询字符串

    在某些情况下,您可能更喜欢使用查询字符串 URL:
    index.php?c=products&m=view&id=345

    c === 你的控制器名称 m === 你的方法名

    剩下的是方法参数。这是一个非常具有误导性的描述,并且在 URL 文档的其余部分中根本没有提及其他设置或查询字符串。我从来没有听说过有人真正使用它。 CI 默认自带$config['allow_get_array']= TRUE;,这就是你想要的。

    您可以修改current_url() 函数以支持查询字符串,只需创建application/helpers/MY_url_helper.php 并使用它:

    function current_url($query_string = FALSE)
    {
        $CI =& get_instance();
        $current_url = $CI->config->site_url($CI->uri->uri_string());
        
        // BEGIN MODIFICATION
        if ($query_string === TRUE)
        {
            // Use your preferred method of fetching the query string
            $current_url .= '?'.http_build_query($_GET);
        }
        // END MODIFICATION
    
        return $current_url;
    }
    

    然后像 current_url(TRUE) 这样调用它以包含查询字符串。

    【讨论】:

      【解决方案2】:

      不要使用:$config['enable_query_strings'] = TRUE;

      改用这个:$config['allow_get_array']= TRUE;

      enable_query_strings不是你想的那样,用的不多。

      要构建您自己的查询字符串,请使用以下两种之一:

      $query_string = http_build_query($this->input->get());
      $query_string = $this->input->server('QUERY_STRING');
      

      还有这个:

      $lastseg_with_query = $lastseg.'?'.$query_string;
      

      请参阅此 SO Q&A 了解更多信息:URI Segment with Question Mark

      【讨论】:

      • 它与上面@WesleyMurch 的答案中的$current_url 相同。他的回答对你来说没有意义吗?这是对您想要的更清晰的描述。
      猜你喜欢
      • 2017-04-23
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多