如何在Codeigniter的索引函数中传递参数 [英] How to pass parameters in index function in codeigniter
问题描述
这是url的示例: http://www.example.com/search/search-key
here's an example of url: http://www.example.com/search/search-keyword
我正在尝试使其工作,我使用.htaccess删除了index.php,搜索是控制器,我想在index方法中传递参数.
I am trying to make this work, I removed the index.php using .htaccess, search is the controller and I want to pass a parameter in the index method.
这是当前的样子:
class Search extends CI_Controller{
function index($param){
echo $param;
}
}
有什么建议吗?我似乎无法使其正常工作
any suggestions? I can't seem to make it work
推荐答案
- 您需要
index
段http://www.example.com/search/index/search-keyword
. - 或者您需要使用路线
$route['search/(:any)'] = 'search/index/$1';
- 或者您可以查看重新映射
- You need the
index
segmenthttp://www.example.com/search/index/search-keyword
. - Or you need to use a route
$route['search/(:any)'] = 'search/index/$1';
- Or you can look at remap
请记住不要信任用户输入,尤其是当您将其输入到URL中时. CI的最新版本现在支持$_GET
变量,因此您可能要考虑使用它或 flashdata .像O' Brien
这样简单的searh术语将给您带来致命错误(您提交的URI不允许使用字符.").
Remember not to trust user input, especially when you are throwing it into your url. The latest version of CI supports $_GET
variables now, so you may want to look into using that or flashdata. A searh term as simple as O' Brien
will give you a fatal error ("The URI you submitted has disallowed characters.").
这篇关于如何在Codeigniter的索引函数中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!