I Laravel version used here is 5.6, routing position in routes / web.php in, so we add a route we want to add in this file.
1. Basic Routing
1
2
3
4
5
6
7
8
|
//get请求,结果如下图
Route::get(
'basic1'
,
function
(){
return
'Hello World'
;
});
//post请求,这里不展示结果图
Route::post(
'basic2'
,
function
(){
return
'Post'
;
});
|
2. Multi-route requests
1
2
3
4
5
6
7
8
|
//自定义多请求,自定义的请求放在下面的数组中
Route::match([
'get'
,
'post'
],
'multy'
,
function
(){
return
"多请求路由"
;
});
//响应所有请求
Route::any(
'multy2'
,
function
(){
return
'响应所有请求'
;
});
|
Custom multi-request
responding to all requests
3. routing parameters
1
2
3
4
|
//必选参数
Route::get(
'user/{id}'
,
function
(
$id
){
return
'用户的id是'
.
$id
;
});
|
1
2
3
4
|
//可选参数,无参数默认值为Doubly
Route::get(
'name/{name?}'
,
function
(
$name
=
'Doubly'
){
return
'用户名为'
.
$name
;
});
|
Parameters for the kit
has no parameters
1
2
3
4
|
//字段验证,名字必须为字母
Route::get(
'name/{name?}'
,
function
(
$name
=
'Doubly'
){
return
'用户名为'
.
$name
;
})->where(
'name'
,
'[A-Za-z]+'
);
|
When the argument is not alphabetic
1
2
3
4
|
//多个参数,并且带有参数验证
Route::get(
'user/{id}/{name?}'
,
function
(
$id
,
$name
=
'Doubly'
){
return
"ID为{$id}的用户名为{$name}"
;
})->where([
'id'
=>
'\d+'
,
'name'
=>
'[A-Za-z]+'
]);
|
4. Routing alias
1
2
3
4
|
//路由别名
Route::get(
'user/center'
,[
'as'
=>
'center'
,
function
(){
return
'路由别名:'
.route(
'center'
);
}]);
|
What are the benefits of using aliases is it?
When we need to modify the route of the time, such as theuser/center
changeuser/member-center
of time, we use the coderoute('cneter')
generated is no need to modify the URL.
6. Routing Groups
1
2
3
4
5
6
7
8
9
10
|
//路由群组
Route::group([
'prefix'
=>
'member'
],
function
(){
Route::get(
'basic1'
,
function
(){
return
'路由群组中的basic1'
;
});
Route::get(
'basic2'
,
function
(){
return
'路由群组中的basic2'
;
});
});
|
By laravel.test / member / basic2 access
output view route 7.
1
2
3
4
|
//路由中输出视图
Route::get(
'view'
,
function
(){
return
view(
'welcome'
);
});
|
welcome.blade.php template content
1
|
<h1>这是路由中输出的视图</h1>
|