1

假设我的页面在:

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 does not show query strings对我有帮助。

我正在使用 CI 2.1.0。

4

2 回答 2

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)包含查询字符串一样调用它。

于 2012-05-07T05:49:25.343 回答
0

不要使用:$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 段

于 2012-05-07T02:46:18.457 回答