Codeigniter从URL获取参数 [英] Codeigniter get parameters from url
问题描述
我有一个网址" http://localhost/codeigniter/index.php/manual_export/一个>".我需要从假定为id的url中获取最后一段.因此,例如" http://localhost/codeigniter/index.php/manual_export/2 ".我需要获取最后一个段为"2".
I have a url "http://localhost/codeigniter/index.php/manual_export/". I need to get last segment from the url which is suppose to be a id. So for example "http://localhost/codeigniter/index.php/manual_export/2". I need to get the last segment which is "2".
我厌倦了使用以下代码:
I tired to use following code:
$id = end($this->uri->segment_array());
当我不在URL中添加"2"并给我"manual_export"时,此方法有效.但是,当我将id传递给url时,出现错误找不到您请求的页面.".我认为这与路由有关.我该如何解决该错误.
This works when I don't add "2" to the url and gives me "manual_export". However when I pass the id to the url I get an error "The page you requested was not found.". I think this is to do with routing. How can I fix this error.
推荐答案
另一种方法是定义路线,然后将其转换为参数
the other way to do it is by defining a route, it will then be converted to a param
例如,如果您的控制器名为manual_export且方法为getrecord
so for example if your controller is called manual_export and the method is getrecord
在文件application/routes.php中
in the file application/routes.php
$route['manual_export/(:any)'] = "manual_export/getrecord/$1";
在您的控制器中manual_export
in your controller manual_export
function getrecord($id){ // etc etc }
这篇关于Codeigniter从URL获取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!