路由代码写在Http下的routes.php中
//基础路由
Route::get('basic1',function(){
return 'hello world';
});
Route::post('basic2',function(){
return 'hello world';
});
//多请求路由
//match 注册一个指定的多路由请求
Route::match(['get','post'],'multy1',function(){
return 'hello multy1';
});
//any 注册一个任意路由请求
Route::any('multy2',function(){
return 'hello multy2';
});
//路由参数
Route::get('user/{id}',function($id){
return 'User-'.$id;
});
//默认路由参数
Route::get('user1/{name?}',function($name='aaa'){
return 'user1-'.$name;
})->where('name','[a-zA-Z]+');//通过正则表达式来规定参数
Route::get('user2/{first}/{name?}',function($first,$name='aaa'){
return 'user2-first-'.$first.' '.'name-'.$name;
})->where(['first'=>'[a-b]+','name'=>'[a-zA-Z]+']);
//路由别名 center为member-center的别名
Route::get('user3/member-center',['as'=>'center',function(){
return 'member-center'.route('center'); //route()生成当前路由
}]);
//路由群组 路由前缀 member为路由组的名字 eg:/member/sjh/2
Route::group(['prefix'=>'member'],function(){
Route::get('sjh/{id}',function(){
return 'sjh';
})->where('id','[1-9]');
Route::get('vip/{id}',function(){
return 'svip';
});
});
//路由中输出视图
Route::get('view',function(){
return view('welcome');
});
//路由与控制器进行关联
//Route::get('路由名称','控制器名称');
//方法一
//Route::get('member/info','MemberController@info');
//方法二
//Route::get('member/info',['uses'=>'MemberController@info']); //数组内必须为uses