标签:tool lan item 技术 word 群组 title com 基础
我这里使用的Laravel版本是5.6,路由位置在routes/web.php中,所以我们在这个文件中添加我们想要添加的路由。
1.基础路由
|
1
2
3
4
5
6
7
8
|
//get请求,结果如下图Route::get(‘basic1‘,function (){ return ‘Hello World‘;});//post请求,这里不展示结果图Route::post(‘basic2‘,function (){ return ‘Post‘;}); |
2.多请求路由
|
1
2
3
4
5
6
7
8
|
//自定义多请求,自定义的请求放在下面的数组中Route::match([‘get‘,‘post‘],‘multy‘,function(){ return "多请求路由";});//响应所有请求Route::any(‘multy2‘,function (){ return ‘响应所有请求‘;}); |
自定义多请求
响应所有请求
3.路由参数
|
1
2
3
4
|
//必选参数Route::get(‘user/{id}‘,function ($id){ return ‘用户的id是‘.$id;}); |
|
1
2
3
4
|
//可选参数,无参数默认值为DoublyRoute::get(‘name/{name?}‘,function ($name = ‘Doubly‘){ return ‘用户名为‘.$name;}); |
参数为kit
没有参数 
|
1
2
3
4
|
//字段验证,名字必须为字母Route::get(‘name/{name?}‘,function ($name = ‘Doubly‘){ return ‘用户名为‘.$name;})->where(‘name‘,‘[A-Za-z]+‘); |
|
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.路由别名
|
1
2
3
4
|
//路由别名Route::get(‘user/center‘,[‘as‘=>‘center‘,function(){ return ‘路由别名:‘.route(‘center‘);}]); |

使用别名的好处是什么呢?
当我们需要修改路由的时候,比如将user/center改成user/member-center的时候,我们代码中使用route(‘cneter‘)生成的URL是不需要修改的。
6.路由群组
|
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‘; });}); |
通过laravel.test/member/basic2访问
7.路由中输出视图
|
1
2
3
4
|
//路由中输出视图Route::get(‘view‘,function(){ return view(‘welcome‘);}); |
welcome.blade.php模板内容
|
1
|
<h1>这是路由中输出的视图</h1> |

标签:tool lan item 技术 word 群组 title com 基础
原文地址:https://www.cnblogs.com/yscgda54/p/11503251.html