Laravel 學習總結 一路由

alongsuotaopaole發表於2020-02-18

路由分類

1.基礎路由

Route::get('user-learn',function(){
return 'user-learn';
});

2.多請求路由

Route::match(['get','post'],'user-learn-multy',function(){
return 'user-learn-multy';
});

Route::any('user-learn-multy-any',function(){
return 'user-learn-multy-any';
});

3.路由引數

Route::get('user-learn/{name}',function($name){
return 'user-learn'.$name;
});
//有預設值
Route::get('user-learn/{name?}',function($name='xiaoming'){
return 'user-learn-'.$name;
});
//路由正規表示式
Route::get('user-learn/{name?}',function($name='xiaoming'){
return 'user-learn-'.$name;
})->where('name','[A-Za-z]+');
//多引數正則路由
Route::get('nihao/zheng/{id}/{name?}',function($id,$name='xiao'){
    return 'user/zheng-'.$name.'-id-'.$id;
})->where(['id'=>'[0-9]+','name'=>'[A-Za-z]+']);

4.路由別名

Route::get('other-name',['as'=>'other',function(){
return router('other');
}]);

5.路由群組

Route::group(['prefix'=>'class'],function(){ Route::get('nihao/zheng/{id}/{name?}',function($id,$name='xiao'){
    return 'user/zheng-group-'.$name.'-id-'.$id;
})->where(['id'=>'[0-9]+','name'=>'[A-Za-z]+']);
});

6.路由中輸出檢視

Route::get('hello',function(){
  return view('hello');//hello 檢視檔案
})
本作品採用《CC 協議》,轉載必須註明作者和本文連結