5

根据 CI 的文档,CodeIgniter 使用基于段的方法,例如:

example.com/my/group

如果我想找到一个特定的组(id=5),我可以访问

example.com/my/group/5

在控制器中,定义

function group($id='') {
    ...
    }

现在我想使用传统的方法,CI 称之为“查询字符串”URL。例子:

example.com/my/group?id=5

如果我直接访问这个 URL,我会得到一个404 page not found。那么我该如何启用它呢?

4

10 回答 10

10

为了可靠地使用查询字符串,我发现您需要做 3 件事

  1. application/config/config.php集合$config['enable_query_strings'] = true;
  2. 再次在application/config/config.php集合$config['uri_protocol'] = "PATH_INFO";
  3. 更改您的 .htaccess 以删除 ? (如果存在)在重写规则中

我使用以下

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
于 2010-05-24T15:16:40.420 回答
6
//Add this method to your (base) controller :
protected function getQueryStringParams() {
    parse_str($_SERVER['QUERY_STRING'], $params);
    return $params;
}


// Example : instagram callback action
public function callback()
{
    $params = $this->getQueryStringParams();
    $code = !empty($params['code']) ? $params['code'] : '';

    if (!empty($code))
    {
        $auth_response = $this->instagram_api->authorize($code);

        // ....  
    }

    // .... handle error
}    
于 2012-05-09T15:28:14.130 回答
1

这可能对某些人有所帮助;将其放入控制器的构造函数中,以逐个控制器重新填充 $_GET(例如,如果您正在集成依赖于 $_GET 的第三方库 - 例如大多数 PHP OAuth 库)。

parse_str(str_replace($_SERVER['QUERY_STRING'],'',$_SERVER['REQUEST_URI']),$_GET);
于 2011-04-29T11:14:06.010 回答
1

您可以 URI PROTOCOL更改config file

  $config['uri_protocol']   = "ORIG_PATH_INFO"; 

  $config['enable_query_strings'] = FALSE;

它将接受查询字符串并允许您的 URL。为我工作:)

于 2013-04-01T10:49:23.690 回答
1

html:

<a href="?accept=1" class="btn btn-sm btn-success">Accept</a>

控制器功能

if ($this->input->get('accept')!='')
{
    $id = $this->input->get('accept', TRUE );
    $this->camprequests->accept($id);
    redirect('controllername/functionname');
}

模型功能

public function accept($id)
{
    $data = array('status'=>'1');
    $this->db->where('id','1');

    if($this->db->update('tablename',$data)) {

        $this->session->set_flashdata("accpeted","<div class='col-sm-12 alert alert-success'>Accpeted successfully.</div>"); 

    } else { 

        $this->session->set_flashdata("accpeted","<div class='col-sm-12 alert alert-success'>Error..</div>");  
    }
}
于 2016-12-02T04:28:05.843 回答
0

application/config.php在该行修改:

$config['enable_query_strings'] = FALSE;

相反,让这成为现实。您还必须注意其他细节。见这里

于 2010-05-24T01:15:12.307 回答
0

在 config.php 文件中设置后$config['enable_query_strings'] = TRUE;,您可以将基于段的方法与查询字符串结合使用,但前提是您在查询字符串中使用 2 个或更多变量(由“&”分隔),如下所示:

example.com/my/group?id=5&var=something

有关更多信息,请参阅此答案

于 2010-05-24T03:07:49.663 回答
0

CodeIgniter 可选地支持此功能,可以在您的 application/config.php 文件中启用。如果你打开你的配置文件,你会看到这些项目:

enter code here $config['enable_query_strings'] = FALSE;

$config['controller_trigger'] = 'c'; $config['function_trigger'] = 'm';

如果您将“enable_query_strings”更改为 TRUE,此功能将变为活动状态。

于 2012-05-07T07:28:24.650 回答
0

这实际上是经过测试和确认的

它适用于您喜欢的任何方法;让您自由混合匹配查询字符串和/段方法(与以前的响应相反)

要么你想使用:

example.com/my/group/?id=5

(请注意尾随 / 之前?)。或者

 example.com/my/group/5 

(取决于路由器文件中的 url 模式定义)。甚至

example.com/index.php/?my/group/?id=5

(虽然这看起来很尴尬)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

并在你 codigniter 的 config/config.php 文件中,设置

$config['uri_protocol'] = 'AUTO';
$config['enable_query_strings'] = TRUE;
于 2014-08-13T09:07:29.220 回答
0

我已经尝试了这些步骤,并且在分页中设置查询对我来说是正确的(codeigniter 3)

在控制器中:只需添加这些代码。

$config['enable_query_strings'] = TRUE;

$config['page_query_string'] = TRUE;

$config['query_string_segment'] = 'page'; // you can jot down what you want instead of page in this one. and you could get by this name your $_GET method. So, if you don't use this line, it will automatically create a query with "per_page" and this will be your query. 

相关链接: https ://codeigniter.com/userguide3/libraries/pagination.html#customizing-the-pagination

甚至,如果您想将自己的特定类添加到(由分页创建的标签),您必须添加以下代码以拥有自己喜欢的分页链接样式。

$config['attributes'] = array('class' => 'page-link'); // this is the example of the above and you can write what you want instead of 'page-link' as the value of class.

当我们想将 page-link 类作为 boostrap 样式添加到 a 标签时,这将非常有用。

结果:<a href="home?action=questions&amp;page=12" class="page-link" data-ci-pagination-page="5" dideo-checked="true">5</a>

如您所见,页面链接已添加到此标签中。

相关链接: https ://codeigniter.com/userguide3/libraries/pagination.html#adding-attributes-to-anchors

于 2020-05-18T20:03:28.673 回答