php 如何让 CodeIgniter 接受“查询字符串”URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2894250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make CodeIgniter accept "query string" URLs?
提问by zihaoyu
According to CI's docs, CodeIgniter uses a segment-based approach, for example:
根据 CI 的文档,CodeIgniter 使用基于段的方法,例如:
example.com/my/group
If I want to find a specific group (id=5), I can visit
如果我想找到一个特定的组(id=5),我可以访问
example.com/my/group/5
And in the controller, define
在控制器中,定义
function group($id='') {
...
}
Now I want to use the traditional approach, which CI calls "query string" URL. Example:
现在我想使用传统方法,CI 称之为“查询字符串”URL。例子:
example.com/my/group?id=5
If I go to this URL directly, I get a 404 page not found. So how can I enable this?
如果我直接访问这个 URL,我会得到一个404 page not found。那么我怎样才能启用它呢?
回答by WeeJames
For reliable use of query strings I've found you need to do 3 things
为了可靠地使用查询字符串,我发现您需要做 3 件事
- In
application/config/config.phpset$config['enable_query_strings'] = true; - Again in
application/config/config.phpset$config['uri_protocol'] = "PATH_INFO"; - Change your .htaccess to remove the ? (if present) in the rewrite rule
- 在
application/config/config.php集$config['enable_query_strings'] = true; - 再次入
application/config/config.php组$config['uri_protocol'] = "PATH_INFO"; - 更改您的 .htaccess 以删除 ? (如果存在)在重写规则中
I use the following
我使用以下
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
回答by Nassif Bourguig
//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
}
回答by Adam Frisby
This might help some people; put this into your controller's constructor to repopulate $_GET on a controller-by-controller basis (e.g. if you are integrating a third party lib that relies on $_GET - such as most PHP OAuth libraries).
这可能会帮助一些人;将其放入控制器的构造函数中,以逐个控制器地重新填充 $_GET(例如,如果您正在集成依赖于 $_GET 的第三方库 - 例如大多数 PHP OAuth 库)。
parse_str(str_replace($_SERVER['QUERY_STRING'],'',$_SERVER['REQUEST_URI']),$_GET);
回答by Alex X Nguyen
You may change
URI PROTOCOLin your config fileto
你可以改变
URI PROTOCOL你config file对
$config['uri_protocol'] = "ORIG_PATH_INFO";
and
和
$config['enable_query_strings'] = FALSE;
It'll accept query strings and allow your URLs. Worked for me :)
它将接受查询字符串并允许您的 URL。为我工作:)
回答by PHP Ninja
Html:
网址:
<a href="?accept=1" class="btn btn-sm btn-success">Accept</a>
Controller Function
控制器功能
if ($this->input->get('accept')!='')
{
$id = $this->input->get('accept', TRUE );
$this->camprequests->accept($id);
redirect('controllername/functionname');
}
Model Function
模型函数
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>");
}
}
回答by Pouria
I have tried these steps and it was correct for me to set a query in pagination (codeigniter 3)
我已经尝试过这些步骤,在分页中设置查询对我来说是正确的(codeigniter 3)
in controller: just add these codes.
在控制器中:只需添加这些代码。
$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.
related link: https://codeigniter.com/userguide3/libraries/pagination.html#customizing-the-pagination
相关链接:https: //codeigniter.com/userguide3/libraries/pagination.html#customizing-the-pagination
Even, if you want to add your own specific class to the (a tags that are created by pagination), you must add the following code to have your own favorite style for pagination links.
甚至,如果您想将您自己的特定类添加到(由分页创建的标签),您必须添加以下代码以获得您自己喜欢的分页链接样式。
$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.
This will be very useful when we want to add page-link class as boostrap style to the a tag.
当我们想要将 page-link 类作为 boostrap 样式添加到 a 标签时,这将非常有用。
result: <a href="home?action=questions&page=12" class="page-link" data-ci-pagination-page="5" dideo-checked="true">5</a>
结果: <a href="home?action=questions&page=12" class="page-link" data-ci-pagination-page="5" dideo-checked="true">5</a>
as you see page-link was added to this a tag.
如您所见,页面链接已添加到此标签中。
related link: https://codeigniter.com/userguide3/libraries/pagination.html#adding-attributes-to-anchors
相关链接:https: //codeigniter.com/userguide3/libraries/pagination.html#adding-attributes-to-anchors
phpcodeigniterpaginationpagination-bootstrappagination-in-query-based-paged
php codeigniter pagination pagination-bootstrap pagination-in-query-based-paged
回答by tormuto
This is actually tested and confirmed
这实际上是经过测试和确认的
It works with any method you like; giving you freedom to mix match the query string and / segment approach (as opposed to the previous responses)
它适用于您喜欢的任何方法;让您可以自由混合匹配查询字符串和/段方法(与之前的响应相反)
either you want to use:
要么你想使用:
example.com/my/group/?id=5
(please note the trailing / before ?). OR
(请注意尾随 / 之前?)。或者
example.com/my/group/5
(depending on your url pattern definitions in the router file). OR EVEN
(取决于您在路由器文件中的 url 模式定义)。甚至
example.com/index.php/?my/group/?id=5
(though that looks awkward enough)
(虽然看起来够别扭)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
and in you codigniter's config/config.php file, set
并在您的 codigniter 的 config/config.php 文件中,设置
$config['uri_protocol'] = 'AUTO';
$config['enable_query_strings'] = TRUE;
回答by GkDreamz
CodeIgniter optionally supports this capability, which can be enabled in your application/config.php file. If you open your config file you'll see these items:
CodeIgniter 可选择支持此功能,可以在您的 application/config.php 文件中启用。如果您打开配置文件,您将看到以下项目:
enter code here $config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c'; $config['function_trigger'] = 'm';
$config['controller_trigger'] = 'c'; $config['function_trigger'] = 'm';
If you change "enable_query_strings" to TRUE this feature will become active.
如果您将“enable_query_strings”更改为 TRUE,则此功能将变为活动状态。
回答by wallyk
回答by Colin Brock
After setting $config['enable_query_strings'] = TRUE;in your config.php file, you can use the segment-based approach in conjunction with query strings, but only if you use 2 or more variables (separated by a "&") in the query stringlike this:
在$config['enable_query_strings'] = TRUE;config.php 文件中设置后,您可以将基于段的方法与查询字符串结合使用,但前提是您在查询字符串中使用 2 个或多个变量(由“&”分隔),如下所示:
example.com/my/group?id=5&var=something
See this answerfor more information.

