所有 Laravel 路由都定義在路由文件中,這些文件位於 routes 目錄下。
routes/web.php 文件定義路由開始的。 可以通過在瀏覽器中輸入定義的路由 URL 來訪問 routes/web.php 的路由。
routes/api.php 文件中定義的路由通過 RouteServiceProvider 被嵌套到一個路由組裏面。在這個路由組中,會自動添加 URL 前綴 /api 到此文件中的每個路由,這樣你就無需再手動添加了。
路由動詞
Route::get($uri, $callback); Route::post($uri, $callback); Route::put($uri, $callback); Route::patch($uri, $callback); Route::delete($uri, $callback); Route::options($uri, $callback);
基本路由
use Illuminate\Support\Facades\Route; Route::get('/greeting', function () { return 'Hello World'; });//閉包函數
Route::get('/pay-order','PayController@payOrder');//直接調用控制器
Route::match(['get', 'post'], '/', function () {
//
});
Route::any('/', function () {
//
});
路由參數
Route::get('/user/{id}', function ($id) {
return 'User '.$id;
});
//多個參數
Route::get('/posts/{post}/comments/{comment}', function ($postId, $commentId) { // });
//可選參數,有時,你可能需要指定一個路由參數,但你希望這個參數是可選的。你可以在參數後面加上?
標記來實現,但前提是要確保路由的相應變量有默認值
Route::get('/user/{name?}', function ($name = null) {
return $name;
});
Route::get('/user/{name?}', function ($name = 'John') {
return $name;
});
正則約束
Route::get('/user/{name}', function ($name) { // })->where('name', '[A-Za-z]+'); Route::get('/user/{id}', function ($id) { // })->where('id', '[0-9]+'); Route::get('/user/{id}/{name}', function ($id, $name) { // })->where(['id' => '[0-9]+', 'name' => '[a-z]+']);
路由重命名
Route::get('/user/profile', function () { // })->name('profile');
路由助手
users/{uiserd}/comments/{commentId} 方法1 route('users.comments.show',[1,2]);//http://myapp.com/users/1/comments/2 方法2 route('users.comments.show',['userid'=>1,'commentId'=>2]);//http://myapp.com/users/1/comments/2
方法3
route('users.comments.show',['userid'=>1,'commentId'=>2,'opt'=>'abc']);//http://myapp.com/users/1/comments/2?opt=abc
路由分組
Route::groupgroup(['prefix'=>'login'],function ($route) {
$route->post('/user-login','LoginController@userLogin');// 用戶登錄
$route->post('/refresh-token','LoginController@refreshToken');// 刷新token
$route->post('/forget-password-get-code','LoginController@forgetPasswordGetCode');// 修改登錄密碼
$route->post('/reset-password','LoginController@resetPassword');// 重置密碼
});
子域路由
Route::domain('{account}.example.com')->group(function () { Route::get('user/{id}', function ($account, $id) { // }); });
中間件
Route::middleware(['first', 'second'])->group(function () { Route::get('/', function () { // Uses first & second middleware... }); Route::get('/user/profile', function () { // Uses first & second middleware... }); }); Route::group(['middleware'=>'auth'],function(){ Route::get('/', function () { // Uses first & second middleware... }); Route::get('/user/profile', function () { // Uses first & second middleware... }); })
路徑前綴
Route::prefix('admin')->group(function () { Route::get('/users', function () { // Matches The "/admin/users" URL }); }); Route::group(['prefix'=>'admin'],function(){ Route::get('/users', function () { // Matches The "/admin/users" URL }); })
名稱前綴
Route::name('admin.')->group(function () { Route::get('/users', function () { // Route assigned name "admin.users"... })->name('users'); });
命名空間前綴
Route::get('/','ControllerA@index'); Route::group(['namespace'=>'API'], function(){ Route::get('api/','ControllerB@index'); });