Skip to content

Commit cd9478f

Browse files
author
qiang.sun
committed
路由分组
1 parent 03af923 commit cd9478f

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

routes/web.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
return "Hello, World!";
1616
});
1717

18+
// 路由参数
1819
Route::get('user/{id?}', function ($id = 1) {
1920
return "用户ID: " . $id;
2021
})->name('user.profile');
@@ -31,6 +32,61 @@
3132
return $id . ':' . $slug;
3233
})->where(['id' => '[0-9]+', 'slug' => '[A-Za-z]+']);
3334

35+
// 路由分组
36+
Route::group([], function () {
37+
Route::get('hello', function () { return 'Hello'; });
38+
Route::get('world', function () { return 'World'; });
39+
});
40+
41+
// 中间件
42+
Route::middleware('auth')->group(function () {
43+
Route::get('dashboard', function () {
44+
return view('dashboard');
45+
});
46+
Route::get('account', function () {
47+
return view('account');
48+
});
49+
});
50+
51+
// 路由前缀
52+
Route::prefix('api')->group(function () {
53+
Route::get('/', function () {
54+
// 处理 /api 路由
55+
})->name('api.index');
56+
Route::get('users', function () {
57+
// 处理 /api/users 路由
58+
})->name('api.users');
59+
});
60+
61+
// 子域名
62+
Route::domain('api.blog.test')->group(function () {
63+
Route::get('/', function () {
64+
// 处理 http://api.blog.test 路由
65+
});
66+
});
67+
68+
Route::domain('{account}.blog.test')->group(function () {
69+
Route::get('/', function ($account) {
70+
//
71+
});
72+
Route::get('user/{id}', function ($account, $id) {
73+
//
74+
});
75+
});
76+
77+
// 命名空间
78+
79+
// 路由命名+路径前缀
80+
Route::name('user.')->prefix('user')->group(function () {
81+
Route::get('{id?}', function ($id = 1) {
82+
// 处理 /user/{id} 路由,路由命名为 user.show
83+
return route('user.show');
84+
})->name('show');
85+
Route::get('posts', function () {
86+
// 处理 /user/posts 路由,路由命名为 user.posts
87+
})->name('posts');
88+
});
89+
3490
Route::get('test', function () {
35-
return route('user.profile', [100]);
91+
return route('user.show', [100]);
3692
});

0 commit comments

Comments
 (0)