路由設定在有無路由快取時結果不同。已在 Laravel 10 和 11 測試過,有相同結果

use App\Http\Controllers\HomeController;
// A
Route::get('/', [HomeController::class, 'get']);
Route::match(['get', 'post'], '/', [HomeController::class, 'match']);
Route::any('/', [HomeController::class, 'any']);
// B
Route::get('/', [HomeController::class, 'get']);
Route::any('/', [HomeController::class, 'any']);
Route::match(['get', 'post'], '/', [HomeController::class, 'match']);
// C
Route::any('/', [HomeController::class, 'any']);
Route::get('/', [HomeController::class, 'get']);
Route::match(['get', 'post'], '/', [HomeController::class, 'match']);
// D
Route::any('/', [HomeController::class, 'any']);
Route::match(['get', 'post'], '/', [HomeController::class, 'match']);
Route::get('/', [HomeController::class, 'get']);
路由快取ABCD
沒有Route::any()Route::match()Route::match()Route::get()
Route::match()Route::match()Route::any()Route::any()

官方文件說,get, post 等方法的路由應該在 any, match 方法之前定義,以確保正確的路由匹配。所以路由是由上往下匹配的嗎?以上測試的結果,沒有路由快取時,由下往上匹配;有快取時則不一定。所以最好不要用 Route::any()Route::match()

// A
Route::get('/there', function() {
  return 'there';
});
Route::get('/here', function() {
  return 'here';
});
Route::redirect('/here', '/there');
// B:Route::redirect() 移到最上面
Route::redirect('/here', '/there');
Route::get('/there', function() {
  return 'there';
});
Route::get('/here', function() {
  return 'here';
});

GET /here 時,有無路由快取結果也不同。已在 Laravel 10 和 11 測試過,有相同結果

路由快取AB路由匹配
沒有顯示 there顯示 here由下往上
顯示 here顯示 there由上往下

另外重新導向並不是用 HTML 的 <meta http-equiv="refresh" content="0; url=http://127.0.0.1:8000/">,這點和 Laravel 6 不同。