微信扫一扫打赏支持

thinkphp动态注册路由

thinkphp动态注册路由

一、总结

1、thinkphp使用路由步骤:a、config配置文件中开启路由  b、Route类的rule方法创建路由(在Routephp中)Route::rule('/','index/index/index');

2、带参数的路由:引号+参数:Route::rule('course/:id','index/index/course'); 引号+参数

3、可选参数路由:中括号,和别而通式中的可选一样:Route::rule('time/:year/[:month]','index/index/shijian');

4、带额外参数路由:默认自带参数,Route::rule('test2','Index/index/test2?id=10&name=zhangsan');

5、rule的第三个参数:是路由支持的请求类型,例如get、post等,默认支持所有

 

二、thinkphp动态注册路由

设置路由-动态单个注册

         0、设置路由格式

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

         1、设置路由文件

                   C:\AppServ\www\tp5\application\route.php

 

         2、如何设置

 

                   // 引入系统类

                   use think\Route;

                   // 定义路由规则

                   // 设置路由之后,就不能使用pathinfo访问了

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

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

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

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

 

         3、路由的形式

                   1、静态地址路由

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

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

 

                   2、路由带参数

                            // 注册带参数路由

                            // http://www.tp.com/couser/1

                            // http://www.tp.com/index/index/index/id/1

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

                            // 如果路由设置两个参数,必须带两个参数

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

 

                   3、可选参数路由

                            // http://www.tp.com/time/2017

                            // http://www.tp.com/time/2017/8

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

 

                   4、全动态路由(不建议大家使用)

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

 

                   5、完全匹配路由

                            // http://www.tp.com/test1 #可以成功访问

                            // http://www.tp.com/test1/1 #不能访问

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

 

                   6、路由额外带参数

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

 

         4、设置请求类型

                   1、TP中请求类型

                            get、post、put、delete

 

                   2、Route::rule() 默认支持所有请求类型

 

                   3、设置各种请求

                            // 支持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');

 

                            // 同时支持get和post

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

 

                            // 支持所有路由

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

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

 

                            // 支持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/index/type');

 

                   4、如何模拟put和delete请求

                            <form action="type" method="post">**

                                     <p>

                                               <input type="hidden" name="_method" value="PUT">**

                                               <input type="text" name="name" id="">

                                     </p>

                                     <p>

                                               <input type="submit" value="提交">

                                     </p>

                            </form>

 

posted @ 2018-05-06 16:45  范仁义  阅读(1604)  评论(0编辑  收藏  举报