CodeIgniter框架相关

1. URI 类
须知:
http协议中的URI和URL有什么区别: https://www.zhihu.com/question/21950864

 1 <?php
 2 //假设当前URL为 http://xf.com/index.php/foods/dinner/noodle
 3 
 4 /**
 5  * segment($n[, $no_result=NULL])
 6  * URI路由: rsegment($n[, $no_result=NULL])
 7  * 从URI中获取指定段
 8  */
 9 $arr = $this->uri->segment(2, 0);  //dinner
10 
11 /**
12  * slash _segment($n[, $where='trailing'])
13  * URI路由: slash_rsegment($n[, $where='trailing'])
14  * 根据第二个的参数决定是否在返回结果的前面或者后面添加斜杠
15  */
16 $arr = $this->uri->flash_segment(3);  // noodles
17 $arr = $this->uri->flash_segemnt(3, 'leading');  // /noodle
18 $arr = $this->uri->flash_segment(3, 'both');  // /noodle/
19 
20 /**
21  * uri_to_assoc($n=3[, $default=array()])
22  * URI路由: ruri_to_assoc($n=3[, $default=array()])
23  * 将 URL的段转换为一个包含键值对的关联数组
24  */
25 //假设当前URL为 index.php/user/search/name/joe/location/UK/gender/male
26 $arr = $this->uri->uri_to_assoc(3);
27 $_default = array('name', 'gender', 'location', 'type', 'sort');
28 $arr = $this->uri->uri_to_assoc(3, $_default);
29 //返回结果如下
30 $arr = array(
31     'name' => 'joe',
32     'location' => 'UK',
33     'gender' => 'male'
34 );
35 
36 /**
37  * assoc_to_uri($array)
38  * 根据输入关联数组生成一个URI字符串
39  */
40 $arr = array(
41     'product' => 'shoes',
42     'size' => 'large',
43     'color' => 'red'
44 );
45 $str = $uri_str = $this->uri->assoc_to_uri($arr);  //product/shoes/size/large/color/red
46 
47 /**
48  * uri_string()
49  * URI路由: ruri_string()
50  * 返回一个相对的URI
51  */
52 $str = $this->uri->string();  //foods/dinner/noodle
53 
54 /**
55  * total_segments()
56  * total_rsegments()
57  * 返回URI的总段数
58  */
59 $n = $this->uri->total_segment();
60 
61 /**
62  * segment_array()
63  * rsegment_array()
64  * 返回URI所有的段组成的数组
65  */
66 $arr = $this->uri->segment_array();
67 //结果为:
68 $arr = array(
69     1 => 'food',
70     2 => 'dinner',
71     3 => 'noodle'
72 );
73 
74 ?>
View Code

 2. URI 路由
须知:
正则表达式 http://www.regular-expressions.info/

一般情况下,使用ci框架的web应用,其URL遵循酱紫的规则:
xf.com/class/function/val
另一方面,CodeIgnoter允许使用者重新定义URL的处理流程,在 config/routes.php 里通过修改变量 $route 来设置自己的路由规则。
比如:

 1 <?php
 2 
 3 /*
 4  * 三个保留路由
 5  */
 6 $route['default_controller'] = 'welcome';  //可以设置站点的默认访问页面
 7 $route['404_override'] = '';  //会覆盖默认的404页面,不会影响 show_404()函数
 8 $route['translate_uri_dashed'] = false;  //是否要将URL中控制器和方法名中的'-'转换为'_'
 9 
10 /*
11  数组的键值是匹配的URI,值表示要重定向的位置
12 
13  在配置站点适合的路由时,要注意键值里使用的通配符是否正确:
14  :any  即 [^/]+   除了 '/'以外的任意字符
15  :num  即 [0-9]+   只含有数字
16 
17  不要在值的前面或者后面加反斜杠 '/'
18  */
19 $route['icecream/(:any)'] = 'snack/cooler';
20 
21 /*
22  * 使用回调函数
23  */
24 $route['products/([a-zA-Z]+)/edit/(\d+)'] = function ($product_type, $id)
25 {
26     return 'catalog/product_edit/' . strtolower($product_type) . '/' . $id;
27 };
28 
29 /*
30  * 使用http动词
31  *
32  * 不区分大小写
33  *
34  * 创建RESTful应用时,或许可以酱紫使用路由
35  */
36 $routes['products']['put'] = 'product/insert';
37 $routes['products/(:num)']['DELETE'] = 'product/delete/$1';
38 
39 ?>
View Code

 

posted @ 2017-12-04 14:27  胖小烦  阅读(140)  评论(0编辑  收藏  举报