# CodeIgniter 控制器 - 将 URI 片段传递给方法

**URL:** <https://www.isharkfly.com/t/codeigniter-uri/5060>\
**Category:** PHP\
**Created:** [January 5, 2014, 1:03am UTC](https://www.isharkfly.com/t/codeigniter-uri/5060 "2014-01-05T01:03:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [January 5, 2014, 1:03am UTC](https://www.isharkfly.com/t/codeigniter-uri/5060/1 "2014-01-05T01:03:29Z")

</div>

如果你的 URI 超过两个部分，那么超过的将被作为参数传递给方法。

举例来说，如果你的 URI 是这样的：`example.com/index.php/products/shoes/sandals/123`URI 的第3和第4部分会被传递给你的方法（“sandals” 和 “123”）：`<?php
class Products extends CI_Controller {
public function shoes($sandals, $id)
 {
 echo $sandals;
 echo $id;
 }
}
?>
`上面的方法调用时必须给两个相应的参数，要不然会出错。当然你也可以像下面这样写，就不用在调用的时候给参数了！`<?php
class Products extends CI_Controller {
public function shoes($sandals='Test', $id=1)
 {
 echo $sandals;
 echo $id;
 }
}
?>`注意：如果你使用 URI 路由特性，则传递到方法中的 URI 片段将被重新路由一次。
