15
15
return "Hello, World! " ;
16
16
});
17
17
18
+ // 路由参数
18
19
Route::get ('user/{id?} ' , function ($ id = 1 ) {
19
20
return "用户ID: " . $ id ;
20
21
})->name ('user.profile ' );
31
32
return $ id . ': ' . $ slug ;
32
33
})->where (['id ' => '[0-9]+ ' , 'slug ' => '[A-Za-z]+ ' ]);
33
34
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
+
34
90
Route::get ('test ' , function () {
35
- return route ('user.profile ' , [100 ]);
91
+ return route ('user.show ' , [100 ]);
36
92
});
0 commit comments