目录
1、路由的基本配置位置
路由使用默认是开启的,也可将其关闭,对应开关在config/app.php下的如同所示的地方:
另外路由的基础配置在config/route.php里也可修改配置。
路由的写入文件为route/app.php文件。
编辑时需引入
use think\facade\Route;
2、单条路由的常用操作
开启测试前先规划一下根域名,打开cmd,找到对应目录,输入php think run:此时根域名为http://localhost:8000/。
(1)访问根域名路由,比较特殊的,除此之外的路由都得在第二个参数时写入'controll/method'
Route::rule('/','index');
通过根域名可以直接跳转到index/index对应的路径
(2)只接收get或post等请求跳转的写法
//对应路径http://localhost:8000/howAreYou/12 可以是get和post请求
Route::rule('howAreYou/:id','test/details','GET|POST');//rule方法支持各种请求
//对应路径http://localhost:8000/hello/21 相当于访问了/test/index
Route::get('hello/:name', 'test/index');//只允许get请求跳转
(3)直接跳转并执行方法
//通过http://localhost:8000/think/Jack访问,此时直接返回视图hello,ThinkPHP6!Jack
//:name表示变量名,可自定义
Route::get('think/:name', function ($name) {
return 'hello,ThinkPHP6!'.$name;
});
(4)常用约束限制
Route::get('hello/:name', 'test/index')
->ext('html | shtml')//只允许以.html或.shtml访问,但tp6里一般仅支持.html和域名访问
->ajax()//只允许ajax请求
->https();//只允许https请求
过滤器:
//访问该路由时需携带filter里的参数,不然不能访问
Route::rule('yyy','test/index')->filter(['id'=>1,'type'=>'big']);
//可访问的路径如:http://localhost:8000/yyy?id=1&type=big
追加路由参数:
Route::rule('zzz','test/index')->append(['status'=>1]);//当路由传到时追加append里的参数
option将约束写在一起:
(5)也支持多级目录跳转,如controll/group/hello控制器
//对应路由为http://localhost:8000/hello相当于访问http://localhost:8000/group.hello/index
Route::rule('hello','group.hello/index');
(6)静态路径和动态路径的访问
静态方式访问最后的方法前改用@,只能调用静态方法而不能调用动态方法。但动态路径可以调用静态方法。
//对应访问路径http://localhost:8000/world相当于http://localhost:8000/test/index
Route::rule('world','app\controller\test@index');
(7)域名路由domain
//可通过http://news.wxl0101.com:8000/hello/12访问
//相对于替代了http://localhost:8000/test/index/12
Route::rule('hello/:id','test/index')->domain('news.wxl0101.com');
域名限制
//域名限制访问
//可通过http://news.abc.com:8000/abc访问
Route::domain('news.abc.com',function (){
Route::rule('abc','test/index');
});
//可通过http://news/abc访问
Route::domain('news',function (){
Route::rule('abc','test/index');
});
//可通过http://news.blog.live:8000/abc访问
Route::domain(['news','blog','live'],function (){
Route::rule('abc','test/index');
})->ext('html');//限制约束
(8)跨越请求解决
//允许跨域请求,但仅这样写不安全,所有网站都可访问到,所以要配置参数
Route::rule('hhh','test/index')->allowCrossDomain();
//安全做法如下:
Route::rule('hhh','test/index')->allowCrossDomain([
'Access-Control-Allow-Origin'=>'http://news.abc.com:8000'//表示只允许该网站来取数据
]);
3、分组路由的常用操作
通过分组路由可以对统一的操作有效管理:
Route::group(function (){
Route::rule('hhh','test/index');
Route::rule('wxl/:id','test/details');
})->ext('html');//方便统一约束
//也可以添加,字段,如下访问路径应改在访问前添加group对应'名称/'
//访问路径为http://localhost:8000/test233/i或h
Route::group('test233',function (){
Route::rule('i','test/index');
Route::rule('h','group.hello/index');
})->ext('html');//方便统一约束
//也支持嵌套
//访问路径为http://localhost:8000/parent/next/hhh或wxl/12
Route::group('parent',function (){
Route::group('next',function (){
Route::rule('hhh','test/index');
Route::rule('wxl/:id','test/details');
});
})->ext('html');
4、页面不存在时的操作miss
//这样在该范围内找不到页面时跳转到miss里的页面
Route::miss('index/miss');
//也支持部分页面访问不到时跳转到miss里的页面
Route::group('next',function (){
Route::rule('hhh','test/index');
Route::rule('wxl/:id','test/details');
Route::miss('index/miss');//局部的miss
})->ext('html');
另外我们使用redirect也可实现类似效果:
Route::redirect('mistake','/',302);//302也可省略
//这样写,在访问http://localhost:8000/mistake时,他会直接放回http://localhost:8000/页面,
//并访问302参数
更多使用见官方文档:路由 · ThinkPHP6.0完全开发手册 · 看云