Codeigniter 使用 post 和 get 参数进行路由
我的 config/routes.php 文件中有以下内容:
$route['(:any)'] = "dashboard/index/$1";
访问以下网址时,这将按预期工作:< strong>mysite.com/user/id,调用仪表板控制器中的函数,第一个参数是“user”,第二个参数是“id”。但是,当传递 post 并获取诸如 mysite.com/user/id?page=2 之类的值时,该函数似乎会忽略 post 参数,并且第一个(也是唯一的)参数是“2”。
我的 config.php 文件具有以下值:
$config[‘uri_protocol’] = “AUTO”;
$config[‘enable_query_strings’] = TRUE;
和.htaccess:
RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
我的路线条件中是否缺少某些内容?
I have the following in my config/routes.php file:
$route['(:any)'] = "dashboard/index/$1";
This works as expected when visiting the following url: mysite.com/user/id, the function in the dashboard controller is called and the 1st param is 'user' and 2nd 'id'. However when passing through post and get values such as mysite.com/user/id?page=2 the function appears to ignore the post params and the 1st (and only) param is '2'.
My config.php file has following values:
$config[‘uri_protocol’] = “AUTO”;
$config[‘enable_query_strings’] = TRUE;
and .htaccess:
RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Am I missing something in my route condition?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这绝对是您的自定义路线,您可以尝试 yoursite.com/user/id/?page=2 因为我认为字符串 id?page=2 作为一个参数传递。
您还可以通过打印出传递的 url 键的数组来进行调试:
并且您将获得传递给控制器操作的所有参数。您还可以看到这个答案: Handling Question mark in url in codeigniter 关于扩展核心 URI 类(如果需要)。
我希望这会有所帮助,否则更多信息会有所帮助,例如,当您使用 site.com/user/id 等普通请求以及执行 site.com/user/id 时,控制器中传递的 url 键是什么?页=2 个请求。
****** 编辑 **********
如果您使用的是 CI 1.7.1 或 1.7.2,那么您将得到 [1] => page 因为在 _fetch_uri_string 方法中你有这样的东西:
因为你有 site.com/user/id/?page=2 这意味着: $_GET['page'] = 2 并且 page 是 $_GET 中键的名称
请使用上面关于如何在应用程序中扩展此类的链接,并在此处添加您自己的逻辑,例如类似的内容适用于 1.7.1 或 1.7.2
希望这会有所帮助
It's definitely your custom route, could you please try yoursite.com/user/id/?page=2 cause I think that the string id?page=2 gets passed as one parameter.
You can also do debugging with printing out the array of passed url keys:
and you will get all params passed to your controller action. You can also see this answer: Handling question mark in url in codeigniter about extending the core URI class if that is needed.
I hope this helps, otherwise more info will be helpful, for example what are the url keys passed in your controller when you are using normal request like site.com/user/id and when you are doing site.com/user/id?page=2 requests.
****** EDIT **********
If you are using CI 1.7.1 or 1.7.2 then you are getting [1] => page because in the _fetch_uri_string method you have something like this:
Since you have site.com/user/id/?page=2 this means: $_GET['page'] = 2 and page is the name of the key in $_GET
Please use the link above about how to extend this class in your application and add your own logic here, for example something like this would work for 1.7.1 or 1.7.2
Hope this helps
uri 段在这里对你没有帮助。
获取 page 的值
您应该通过您的方法接口
?我刚刚尝试过
,它输出:
user, id, [2]
uri segments wont help you here.
you should get the value of page with
what does your method interface look like?
I just tried this
and it output this:
user, id, [2]