Laravel 路由与参数冲突

Laravel route conflict with parameter

我正在尝试使用

在 Laravel 中设置路由
Route::get('/post/{id}', 'PostController@index');
Route::get('/post/new', 'PostController@create');

但是当我转到 mysite.com/post/new 它运行索引函数时认为它是一个 {id}。

所以我想知道我是否可以强制 /new 转到创建函数,或者我是否必须将 /post/ 更改为不同的内容。

在此先感谢您的帮助!

Route::get('/post/{id}', 'PostController@index')->where('id', '[0-9]+');

看看:Regular Expression Constraints

同样重要!! .路由声明的顺序很重要。试试这个

Route::get('/post/new', 'PostController@create');
Route::get('/post/{id}', 'PostController@index');

并且您会注意到您的应用能够将 new 识别为与 {id} 不同的路线。
发生这种情况是因为 路由解析器 搜索直到找到与路由

匹配的第一个模式