首先需要在配置中
'URL_PARAMS_BIND' => true, // URL变量绑定到操作方法作为参数
之后控制器中
public function index($id){
echo $id;
}
此控制器位于目录结构为Home下的Controller/IndexController.class.php
为id赋值,则输入
http://localhost/think/index.php/Home/index/index/id/1
需注意的是进行URL的参数绑定时需要把控制器位置详细写清楚;
class BlogController extends Controller{
public function read($id){
echo 'id='.$id;
}
public function archive($year='2013',$month='01'){
echo 'year='.$year.'&month='.$month;
}
}
这种情况下,URL的访问地址分别是:
当然第二个有两个变量的,两个变量在URL中调整顺序也是可以的,但是如果按照变量的顺序绑定,这种情况下URL地址中的参数顺序非常重要,不能随意调整。
http://serverName/index.php/Home/Blog/read/id/5
http://serverName/index.php/Home/Blog/archive/year/2013/month/11
这样做首先需要在配置中增加:
'URL_PARAMS_BIND_TYPE' => 1, // 设置参数绑定按照变量顺序绑定
这样的话可以访问:
效果相同,不过要注意严格遵守顺序
http://serverName/index.php/Home/Blog/read/5
http://serverName/index.php/Home/Blog/archive/2013/11