Action参数绑定

来源:互联网 发布:去掉重复查数据库 编辑:程序博客网 时间:2024/06/05 21:52

首先需要在配置中

'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的访问地址分别是:

    1. http://serverName/index.php/Home/Blog/read/id/5
    2. http://serverName/index.php/Home/Blog/archive/year/2013/month/11
    当然第二个有两个变量的,两个变量在URL中调整顺序也是可以的,但是如果按照变量的顺序绑定,这种情况下URL地址中的参数顺序非常重要,不能随意调整。


    这样做首先需要在配置中增加:

    'URL_PARAMS_BIND_TYPE'  =>  1, // 设置参数绑定按照变量顺序绑定这样的话可以访问:
    1. http://serverName/index.php/Home/Blog/read/5
    2. http://serverName/index.php/Home/Blog/archive/2013/11
    效果相同,不过要注意严格遵守顺序

    0 0