lshe2014 发表于 2016-4-11 10:13:20

设置了路由,方法里面用segment怎么接收参数

配置文件里面路由规则是默认的,如下:
$config['uri_protocol']        = 'AUTO';

配置路由规则如下:
$route['special/(\d+).html'] = "home/special_detail/$1";

访问地址:http://www.test.cn/special/21.html

在控制器里面接收参数id:

function special_detail(){
      $id = $this->uri->segment(3);print_r($id);die;
}
打印结果为空,但是$this->uri->segment(2)打印结果却是21.html,为什么呢?怎么才能用上面的方法接收?

Closer 发表于 2016-4-11 11:13:03

沒記錯的話,可以這樣接收:

function special_detail($id){
      print_r($id);
      die();
}

lshe2014 发表于 2016-4-11 13:02:53

Closer 发表于 2016-4-11 11:13
沒記錯的話,可以這樣接收:

谢谢,这样是可以的,但是想弄明白那样接收为什么不对?呵呵{:1_1:}

Closer 发表于 2016-4-11 14:06:17

本帖最后由 Closer 于 2016-4-11 14:19 编辑

lshe2014 发表于 2016-4-11 13:02
谢谢,这样是可以的,但是想弄明白那样接收为什么不对?呵呵
與其說不對,不如說 $this->uri->segment() 是擷取真實的 URI
static/image/hrline/line3.png
試試 $this->uri->rsegment() 呢 ?

lshe2014 发表于 2016-4-11 19:24:57

Closer 发表于 2016-4-11 14:06
與其說不對,不如說 $this->uri->segment() 是擷取真實的 URI

試試 $this->uri->rsegment() 呢 ? ...

$this->uri->rsegment(3)接收打印出来的就是21,达到想要接收的数据了。
但是我在另一个项目中,也是用segment来接收的,也是按路由 “=” 号右边的次序来接收的,
$this->uri->segment(3)也是21,这是为什么呢?

lshe2014 发表于 2016-4-11 19:26:23

lshe2014 发表于 2016-4-11 19:24
$this->uri->rsegment(3)接收打印出来的就是21,达到想要接收的数据了。
但是我在另一个项目中,也是用se ...

在本例中这样用却接收不到。

Closer 发表于 2016-4-11 19:46:09

lshe2014 发表于 2016-4-11 19:24
$this->uri->rsegment(3)接收打印出来的就是21,达到想要接收的数据了。
但是我在另一个项目中,也是用se ...

也許需要你提供另一個項目的 route 才能得知了

xiaonniqiu2015 发表于 2016-4-12 07:42:41

:victory::victory::victory::victory::victory::victory::victory::victory::victory:

lshe2014 发表于 2016-4-15 19:10:49

Closer 发表于 2016-4-11 19:46
也許需要你提供另一個項目的 route 才能得知了

谢谢,原来是原来那个项目核心ruter被修改了

Hex 发表于 2016-4-16 19:55:43

rsegment 和 segment 有区别的,楼主看看手册吧
页: [1]
查看完整版本: 设置了路由,方法里面用segment怎么接收参数