一 核心概念与启用方式
二 规则定义与匹配机制
'路由规则' => '分组/模块/操作?额外参数''路由规则' => ['分组/模块/操作', '额外参数']'路由规则' => '外部地址'(跳转)'路由规则' => ['外部地址', '重定向码'](如 301)Route::rule('规则', '地址', '请求类型'),或使用快捷方法如 Route::get()/post()。blog/<id>、new/<year>/<month>/<day>。hello/<name?>$。hello/[:name] 或 hello/<name?>;default() 设置默认值:->default(['name'=>'world'])。->pattern(['id'=>'\d+']);三 路由地址与参数控制
'Index/hello'、'group.Blog/details';'\app\controller\Address@details';Route::redirect('ds/:id', 'http://localhost:8000', 302);Route::get('think/:name', function($name){ return 'Hello, '.$name; })。->append(['status'=>1]);前置过滤:->filter(['id'=>5]);->ext('html|shtml');协议限制:->https();->domain('localhost');请求方式:->ajax()/pjax()/json();->allowCrossDomain(['Access-Control-Allow-Origin'=>'*']);->option(['ext'=>'html','https'=>true])。'new/top' => 'Index/top?type=top'。四 分组、资源、注解与 MISS
Route::group('address', function(){
Route::rule('ds/:id', 'details');
Route::rule('rd/:id', 'read');
})->prefix('Address')->ext('html');
Route::resource('blog', 'Blog');(生成 index、read、save、update、delete 等)。/** @route('hello/:name', method='get') */
public function hello($name){ return 'Hello, '.$name; }
Route::miss('public/miss');Route::group('address', function(){ ... })->miss('miss');。五 实战示例与常见问题
return [
// 全局变量规则
'__pattern__' => [
'id' => '\d+',
'year' => '\d{4}',
'month'=> '\d{2}',
],
// 可选参数 + 固定参数 + 后缀限制
'blog/[:id]' => ['blog/read', ['method'=>'get'], ['status'=>1]],
'blog-<year>-<month>' => 'blog/archive',
];
use think\facade\Route;
Route::get('hello/<name?>$', 'Index/hello')
->default(['name'=>'world'])
->ext('html');