1. 路由注册
路由注册需要使用Route类
use think\facade\Route
基础路由定义
# Route::rule('路由表达式' , '路由地址' , '请求方式')
Route::rule('test' , 'test/test' , 'get');
路由请求的快速方法
类型 | 描述 | 快捷方法 |
---|---|---|
GET | get请求 | get |
POST | post请求 | post |
PUT | put请求 | put |
DELETE | delete请求 | delete |
PATCH | patch请求 | patch |
HEAD | head请求 | head |
* | 所有请求 | any |
# post请求
Route::post('test' , 'test/test');
规则表达式
# 规则表达式用 / 分隔 参数可以包括动态变量 :变量 <变量>
Route::get('test/:id' , 'test/test');
# 可选变量 [] 包括起来为可选变量
Route::get('test/[:id]' , 'test/test');
# 访问路径
<hostname>/test/id
# 获取路由传递的参数
public function test($id){
return $id;
}
路由标识
# 快速生成url地址可以在路由定义时生成指定且唯一的标识
Route::post('test' , 'test/test')->name('test_test');
# 调用
url('test_test');
# 如果不用标识 则会以路由地址作为标识
url('test/test');
2. 路由地址
控制器/操作
# 访问的是Test内的test方法
Route::get('test' , 'test/test');
# 支持多级控制器
# 访问的是controller内的Test类test方法
Route::get('test' , 'controller.test/test');
路由到类
# 路由访问不一定是控制器的类 可以访问别的类
# \完整类名@方法名 或 \完整类名::方法名
Route::get('test' , '\app\controller\Test::test');
路由重定向
# Route::redirect('路由表达式' , '重定向地址' , 状态码);
# 跳转到百度首页
Route::get('test' , 'http://baidu.com');
# 跳转到 <hostname>/test/test http状态码为302
Route::get('test' , '/test/test' , 302);
路由闭包
# 路由闭包
Route::get('test' , function(){
return ;
});
# 闭包传递参数
Route::get('test/:id' , function($id){
return 'id : ' . $id;
});
# 依赖注入
Route::get('test/:id' , function(Test $test , $id){
return $test->where('id' , $id)->find;
});
3. 路由参数
参数 | 说明 | 方法名 |
---|---|---|
ext | URL后缀检测,支持匹配多个后缀 | ext |
deny_ext | URL禁止后缀检测,支持匹配多个后缀 | denyExt |
https | 检测是否https请求 | https |
domain | 域名检测 | domain |
complete_match | 是否完整匹配路由 | completeMatch |
model | 绑定模型 | model |
cache | 请求缓存 | cache |
ajax | Ajax检测 | ajax |
pjax | Pjax检测 | pjax |
json | JSON检测 | json |
validate | 绑定验证器类进行数据验证 | validate |
append | 追加额外的参数 | append |
middleware | 注册路由中间件 | middleware |
filter | 请求变量过滤 | filter |
match | 路由闭包检测 | match |
# 用法示例
# 访问后缀必须为html
Route::get('test' , 'test/test')->ext(html);
# 批量设置路由参数
Route::get('test' , 'test/test')->option(['ext' => 'html' , 'https' => true]);
url后缀
# ext() 参数
# false => 禁止伪静态访问
# ' ' => 允许所有后缀访问(默认)
# html => 只允许设置伪静态后缀
# html|php => 允许多个伪静态后缀
# 访问路径<hostname>/test.html 或 <hostname>/test.php
Route::get('test' , 'test/test')->ext('html|php');
# 如果ext方法不传值则表示不支持后缀访问
# denyExt() => 和ext相反
# 如果ext方法不传值则表示必须用后缀访问
域名检测
# domain() 对域名或子域名进行检测
# 对域名进行检测
Route::get('test' , 'test/test')->domain('test.cn');
https
# https() => 必须用https访问
# 访问路径 https://xxxxxx/test
Route::get('test' , 'test/test')->https();
ajax/pjax/json检测
# 必须用ajax访问
Route::get('test' , 'test/test')->ajax();
# pjax json同理
模型绑定
# model() => 绑定模型类
Route::get('test' , 'test/test')->model('\app\model\Test')
4. 路由中间件
# middleware()
Route::get('test' , 'test/test')->middleware(\app\middleware\Auth::class);
# 注册路由全局中间件
# config/route.php 文件内
'middleware' => [
app\middleware\Auth::class,
app\middleware\Check::class,
],
5. 路由分组
# group()
# 访问路径 <hostname>/group/test/
Route::group('group' , function(){
Route::get('test' , 'test/test');
});
# 路由分组 组内路由会继承分分组的属性 例如ext
Route::group('group' , function(){
# test路由后缀必须为html
Route::get('test' , 'test/test');
})->ext('html');
# 路由分组支持嵌套
Route::group(function(){
Route::('shop' , function(){
Route::get('buy' , 'shop/buy');
})
Route::('user' , function(){
Route::get('list' , 'uset/list');
})
})->ext('html');
6. 资源路由
# resource()
Route::resource('test' , 'test');
标识 | 请求类型 | 生成路由规则 | 对应操作方法(默认) |
---|---|---|---|
index | GET | test | index |
create | GET | test/create | create |
save | POST | test | save |
read | GET | test/:id | read |
edit | GET | test/:id/edit | edit |
update | PUT | test/:id | update |
delete | DELETE | test/:id | delete |
7. 注解路由
安装插件
composer require topthink/think-annotation
namenamespace app\controller;
use app\BaseController;
use think\Request;
# 支持路由分组和中间件
/**
* @Group("test",ext="html")
* @Middleware({Auth::class})
*/
class test extends BaseController{
/**
* @param string $name
* @return mixed
* @Route("hello/:name")
*/
public function test($name){
return 'name : ' . $name;
}
# 支持路由方法
/**
* @param string $name
* @return mixed
* @Route("hello/:name" , method="GET")
*/
public function test($name){
return 'name : ' . $name;
}
}
8. Miss路由
# 全局miss路由 => 匹配不到路由规则自动重定向到misss路由
Route::miss('public/miss');
# 只允许get的miss路由
Route::miss('public/miss' , 'get');
# 分组miss
Route::group(function(){
# 当组内路由都不匹配自动跳转到miss路由
Route::get('test' , 'test/test');
Route::miss('public/miss')
})
9. 跨域
# allowCrossDomain()
# 默认定义 :
Access-Control-Allow-Origin:*
Access-Control-Allow-Methods:GET, POST, PATCH, PUT, DELETE
Access-Control-Allow-Headers:Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With
Route::get('test' , 'test/test')->allowCrossDomain();
# 也可以修改header信息
Route::get('test' , 'test/test')->allowCrossDomain([
'Access-Control-Allow-Origin' => 'test.cn',
'Access-Control-Allow-Credentials' => 'true'
]);