php 各种路由分析_thinkphp5路由详解

tp5路由视频https://ke.qq.com/webcourse/index.html#course_id=235325&term_id=100277509&taid=1539754365785917&vid=f1420a94f7r

1.支持三种方式的url解析规则

2.路由只针对应用,不针对模块(路由设置也是针对应用下所有模块)

3.如果某个模块不进行路由解析,需要关闭它

例: //关闭admin模块的路由(注意:写在加载框架引导文件之后,否则报错)

\think\App::route(false);

![](https://box.kancloud.cn/f346f592ae41fcde0e9a8076a148dde7_1041x526.png)

一:普通模式

定义:

完全使用PATH_INFO方式URL;

形式:

http://www.tp.com/admin.php/index/index(默认的,完整的,未设置的)

如何设置:

//配置文件中

~~~

//是否开启路由

'url_route_on' => false,

//是否强制使用路由

'url_route_must' => falese,

~~~

二:混合模式

定义:

开启路由,并使用路由定义+默认PATH_INFO方式混合

如何设置:

~~~

//是否开启路由

'url_route_on' => true,

//是否强制使用路由

'url_route_must' => false,

~~~

三:强制模式

定义:

开启路由,并必须设置路由才能访问

如何设置:

//是否开启路由

'url_route_on' => true,

//是否强制使用路由

'url_route_must' => true,

#### 设置路由

设置路由格式:

`Route::rule('路由表达式','路由地址','请求类型','路由参数(数组)','变量规则(数组)');`

1.动态单个注册

1)配置文件:tp5\application\route.php

2)如何设置:![](https://box.kancloud.cn/5fa6fe8db6055e5878a495c4fef9424a_1722x303.png)

(设置了路由后的页面不能使用PATH_INFO访问)

3)路由的形式

a.静态地址路由

//注册路由test 访问到index模块index控制器test方法

` Route::rule('test','index/index/test');`

b.给路由带参数

//访问到index模块index控制器course方法并携带参数

` Route::rule('course/:id','index/index/course');`

例:

路由写法:http://www.tp.com/course/1

默认写法:http://www.tp.com/index/index/course/id/1

c.携带多个参数

//如果路由设置2个参数,必须写2个参数

//访问到index模块index控制器shijian方法

`Route::rule('time/:year/:month','index/index/shijian');`

d.可选参数路由

//month为可选参数

` Route::rule('time/:year/[:month]','index/index/shijian');`

e.全动态路由(参数1如果是其他方法,会优先访问到其他方法中,所有不建议使用全动态路由)

`Route::rule(':a/:b','index/index/dongtai');`

f.完全匹配路由(后面加上$)

//后面多加参数什么的就无法访问

`Route::rule('test1$','index/index/test1');`

g.带额外参数

`Route::rule('test2','index/index/test2?id=10&name=zhangsan');`

//打印地址栏

dump(input());

4)路由的请求方式

1.get,post,put,delete

2.默认支持所有请求方式

//支持get请求

Route::rule('type','index/index/type','get');

Route::get('type','index/index/type');

//支持post请求

方法一:Route::rule('type','index/index/type','post');

方法二:Route::post('type','index/index/type');

![](https://box.kancloud.cn/0364c2ec5beab03b164511e536370a77_1104x290.png)

//同时支持get和post

`Route::rule('type','index/index/type','get|post');`

//支持put请求类型

Route::rule('type','index/index/type','put');

Route::put('type','index/index/type);

//支持delete请求

Route::rule('type','index/index/type','delete');

Route::delete('type','index/inex/delete);

//支持所有类型

~~~

Route::rule('type','index/index/type','*');

Route::any('type','index/index/type');

~~~

小知识:

如何模拟请求(手册请求伪装)

![](https://box.kancloud.cn/360c01bfa0d0401a53eff1bb96349040_504x171.png)

2.动态批量注册

Route::rule([

'路由规则1' => '路由参数和地址',

'路由规则2'=>['路由地址和参数',匹配参数(数组)','变量规则(数组)']

...

],'','请求类型','匹配参数(数组)','变量规则' );

例:

Route::rule([

"test" => "index/index/test",

"course/:id"=>"index/index/course"

],'','get');

//只支持get

Route::get([

"test"=>"index/index/test",

"course/:id"=>"index/index/course"

]);

3.配置文件批量注册

application\route.php

![](https://box.kancloud.cn/144c0b5da8dcdbc45f5660a7459a4610_897x320.png)

4.变量规则

Route::rule("course/:id/:name","index/index/course","get",[],['id'=>'\d{1,3}','name'=>'\w+']);

注: 正则

\d+ //必须为数字

\d{1,3} //必须为数字,并且1到3位数之间

\w+ //必须为字符串

5.路由参数

Route::rule("course/:id","index/index/course","get",['method'=>'get','ext'=>'html'],[]);

//路由参数method 规定请求方式必须是get

//路由参数ext规定设置路由的后缀必须是html

6.资源路由

//声明

Route::resource(

'blog','index/blog' );

//会自动注册7个路由规则

| 标识 | 请求类型 | 生成路由规则 | 对应操作方法 |

| --- | --- | --- | --- |

| index | ge | blog | index |

| create | get | blog/create | create |

| save | post | blog | save |

| read | get | blog/:id | read |

| edit | get | blod/:id | edit |

| update | put | blod/:id | update |

| delete | delete | blod/:id | delete |

7.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值