~~~
學習知識點:如何定義各種路由
第一步:找到routes.php文件
//第1種基礎路由
Route::get('/', function () {
return view('welcome');
});
//any是表示不分post,get請求,均可
Route::any('300js', function (){
return 'Hello 300js';
});
//只限post請求
Route::post('post', function(){
return 'post';
});
//第2種多路由,可以get,post請求
Route::match(['get', 'post'], 'qq', function(){
return 'qq';
});
//第3種路由參數
Route::get('300js/{id}', function ($id){
return '300js-' . $id;
});
//路由參數帶默認值的
Route::get('number/{id?}', function($id = 100){
return '300js-id-' . $id;
});
//第4種路由參數帶驗證的,支持正則
Route::get('name/{id}/{name}', function($id, $name){
return 'id-' . $id . ',name-' . $name;
})->where(['id' => '[0-9]+', 'name' => '[A-Za-z]+']);
//第5種路由別名,優點:模板改動,地址不變
Route::get('game/goods-up', ['as' => 'goods', function(){
return route('goods');//若路由變,不影響
}]);
Route:get('game/gg', function(){
})->name('goods');//別名
//第6種路由群組
Route::group(['prefix' => 'user'], function(){
Route::get('order_list', function(){
return 'user-order_list';
});
Route::get('mobile', function(){
return 'user-mobile';
});
});
群組路由別名
Route::group(['prefix' => 'go', 'as' => 'admin::'], function () {
Route::get('dashboard', ['as' => 'dashboard', function () {
// 路由名稱為「admin::dashboard」
return route('admin::dashboard');
}]);
});
//第7種路由渲染視圖
Route::get('view', function(){
return view('welcome');
});
//第8種路由傳參
Route::get('foo/{id}', function($id){
return route('foo', ['id' => 10]);
})->name('foo');
//第9種.使用路由組實現不同命名空間下的Controller,并定義路由組的別名
//不同的命名空間下綁定路由
Route::group(['namespace' => 'Center', 'prefix' => 'center', 'as' => 'center::'], function(){
Route::get('show', ['as' => 'show', 'uses' => 'OrderController@show']);
Route::get('display', ['as' => 'display', 'uses' => 'OrderController@display']);
});
~~~