如何使 CodeIgniter 接受“查询字符串”网址?
根据 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 页面未找到。那么我怎样才能启用这个功能呢?
According to CI's docs, CodeIgniter uses a segment-based approach, for example:
example.com/my/group
If I want to find a specific group (id=5), I can visit
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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
为了可靠地使用查询字符串,我发现您需要在
application/config/config.php
中做 3 件事$config['enable_query_strings'] = true;
application/config/config.php
中设置$config['uri_protocol'] = "PATH_INFO";
我使用以下内容
For reliable use of query strings I've found you need to do 3 things
application/config/config.php
set$config['enable_query_strings'] = true;
application/config/config.php
set$config['uri_protocol'] = "PATH_INFO";
I use the following
这可能对某些人有帮助;将其放入控制器的构造函数中,以便在逐个控制器的基础上重新填充 $_GET(例如,如果您正在集成依赖于 $_GET 的第三方库 - 例如大多数 PHP OAuth 库)。
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).
你可能会改变
在您的
配置文件
中添加URI PROTOCOL
,它将
接受查询字符串并允许您的URL。
为我工作:)
You may change
URI PROTOCOL
in yourconfig file
toand
It'll accept query strings and allow your URLs.
Worked for me :)
Html:
控制器功能
模型功能
Html:
Controller Function
Model Function
修改
application/config.php
行:改为 true。您还必须注意其他细节。请参阅此处。
Modify
application/config.php
at the line:Make this true instead. There are other details you'll have to pay attention to also. See here.
在 config.php 文件中设置
$config['enable_query_strings'] = TRUE;
后,您可以将基于段的方法与查询字符串结合使用,但前提是您使用 2 或查询字符串中有更多变量(用“&”分隔),如下所示:请参阅此答案了解更多信息。
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 string like this:See this answer for more information.
CodeIgniter 可以选择支持此功能,可以在 application/config.php 文件中启用该功能。如果打开配置文件,您将看到以下项目:
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
如果将“enable_query_strings”更改为 TRUE,此功能将变为活动状态。
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:
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
If you change "enable_query_strings" to TRUE this feature will become active.
这是经过实际测试和确认的,
它适用于您喜欢的任何方法;让您可以自由地混合匹配查询字符串和 / 段方法(与之前的响应相反),
或者您想要使用:(
请注意后面的 / ?)。或者
(取决于路由器文件中的 url 模式定义)。或者甚至
(虽然这看起来很尴尬)
并且在你的 codigniter 的 config/config.php 文件中,设置
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:
(please note the trailing / before ?). OR
(depending on your url pattern definitions in the router file). OR EVEN
(though that looks awkward enough)
and in you codigniter's config/config.php file, set
我已经尝试过这些步骤,并且在控制器中的分页(codeigniter 3)中设置查询对我来说是正确的
:只需添加这些代码即可。
相关链接:
https://codeigniter.com/userguide3/libraries/pagination.html# customizing-the-pagination
甚至,如果您想将自己的特定类添加到(由分页创建的标签)中,则必须添加以下代码以拥有您自己喜欢的分页链接样式。
当我们想要将页面链接类作为 boostrap 样式添加到 a 标记时,这将非常有用。
结果:
5
如您所见,页面链接已添加到此标记。
相关链接:
https://codeigniter.com/userguide3/libraries/pagination。 html#adding-attributes-to-anchors
php< /a>codeigniter分页pagination-bootstrappagination-in-query-based-paged
I have tried these steps and it was correct for me to set a query in pagination (codeigniter 3)
in controller: just add these codes.
related link:
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.
This will be very useful when we want to add page-link class as boostrap style to the a tag.
result:
<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
phpcodeigniterpaginationpagination-bootstrappagination-in-query-based-paged