Way to create routes Laravel framework Detailed

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' ;
});

get request 
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 
Customize multiple requests 
responding to all requests 
Responding to all requests 
3. routing parameters

?
1
2
3
4
//必选参数
Route::get( 'user/{id}' , function ( $id ){
   return '用户的id是' . $id ;
});

Parameters have 
Write pictures described here 
no parameters 
Write pictures described here

?
1
2
3
4
//可选参数,无参数默认值为Doubly
Route::get( 'name/{name?}' , function ( $name = 'Doubly' ){
   return '用户名为' . $name ;
});

Parameters for the kit 
Write pictures described here 
has no parameters 
Write pictures described here

?
1
2
3
4
//字段验证,名字必须为字母
Route::get( 'name/{name?}' , function ( $name = 'Doubly' ){
   return '用户名为' . $name ;
})->where( 'name' , '[A-Za-z]+' );

When the argument is not alphabetic 
Write pictures described here

?
1
2
3
4
//多个参数,并且带有参数验证
Route::get( 'user/{id}/{name?}' , function ( $id , $name = 'Doubly' ){
   return "ID为{$id}的用户名为{$name}" ;
})->where([ 'id' => '\d+' , 'name' => '[A-Za-z]+' ]);

Write pictures described here 
4. Routing alias

?
1
2
3
4
//路由别名
Route::get( 'user/center' ,[ 'as' => 'center' , function (){
   return '路由别名:' .route( 'center' );
}]);

Write pictures described here

What are the benefits of using aliases is it? 
When we need to modify the route of the time, such as the user/centerchange user/member-centerof time, we use the code route('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 
Write pictures described here 
output view route 7.

?
1
2
3
4
//路由中输出视图
Route::get( 'view' , function (){
   return view( 'welcome' );
});

welcome.blade.php template content

?
1
<h1>这是路由中输出的视图</h1>

Write pictures described here

Guess you like

Origin www.cnblogs.com/yscgda54/p/11503251.html