我刚刚在 Laravel 中启动了一个新项目。我读到 Laravel 应该转到第一个匹配的路线,但在这里它会转到最后一个匹配的路线。如果我删除第三条路线,它将转到第二条路线。为什么会出现这种行为?
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return 'Hello, World!';
});
Route::get('/', function () {
return view('welcome');
});
Route::get('/', function () {
return 'Hello, World 2!';
});
所以你想看的方式就像一本书一样。它会读取第 1、2 和 3 页。它以 3 结尾,这意味着它将向您显示该页。所以它会一直显示“Hello, world 2”
所以你想做的就是按照这些思路来实现它:
Route::get('/one', function () {
return 'Hello, World!';
});
Route::get('/two', function () {
return view('welcome');
});
Route::get('/three', function () {
return 'Hello, World 2!';
});
这里是一个很好的参考,了解控制器的默认设置方式
/**
* Actions Handled By Resource Controller
* Verb URI Action Route Name View
* GET /photos index photos.index index.blade.php
* POST /photos store photos.store (r)edirect to action: 'edit' or 'index' or 'create'
* GET /photos/{photo} show photos.show show.blade.php
* PUT/PATCH /photos/{photo} update photos.update (r)edirect to action: 'edit' or 'index'
* DELETE /photos/{photo} destroy photos.destroy (r)edirect to action: 'index'
*/
所以最后,您的路线可能如下所示
Route::get('/', [WelcomeController::class, 'welcome']);
或者如果您想使用控制器中的所有功能:
Route::resource('/', WelcomeController::class);
// Uses all the default resources available (see actions in reference list).
// Your can also add "->only(['index', 'show']);" to the end or "->exept([]);" the same way