ThinkPHP设置路由-动态单个注册:
一、设置路由格式
Route::rule('路由表达式','路由地址','请求类型','路由参数(数组)','变量规则(数组)')
二、设置路由文件
C:\AppServ\www\tp5\application\route.php
三、设置方法
// 引入系统类use think\Route;
// 定义路由规则
// 设置路由之后,就不能使用pathinfo访问了
// 注册路由 访问到Index模块index控制器index方法
Route::rule('/','index/index/index');
// 注册路由test 访问到Index模块index控制器test方法
Route::rule('test','index/index/test');
四、路由的形式
1、静态地址路由// 注册路由test 访问到Index模块index控制器test方法
Route::rule('test','index/index/test');
public function test(){ return "我是user"; }2、路由带参数
// 注册带参数路由
// http://www.tp.com/couser/1
// http://www.tp.com/index/index/course/1
Route::rule('course/:id','index/index/course');
public function course(){ echo input('id'); }// 如果路由设置两个参数,不许带一个参数
Route::rule('time/:year/:month','index/index/shijian');
public function time(){ echo input('year').''.input('month'); }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');
public function dongtai(){ echo input('a')." ".input('b'); }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');
五、设置请求类型
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');
public function type(){ dump(input()); echo "我是请求测试类型"; return view(); }
<body> <form action="type" method="post"> <p> <input type="text" name="name" id=""> </p> <p> <input type="submit" value="提交"> </p> </form> </body>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>
本文介绍了ThinkPHP框架中动态单个注册路由的方法,包括设置路由格式、指定路由文件、定义具体路由规则和不同请求类型的路由形式,如Route::rule('test','index/index/test')和Route::delete('type','Index/index/type')。"
116709236,10296764,KDE Plasma 5.9回归:全局菜单详细设置指南,"['Linux', 'KDE Plasma', '桌面环境', '全局菜单', 'Qt']
347

被折叠的 条评论
为什么被折叠?



