Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 从Route Web.php中删除/profile/from/profile/{username}关键字无效_Laravel - Fatal编程技术网

Laravel 从Route Web.php中删除/profile/from/profile/{username}关键字无效

Laravel 从Route Web.php中删除/profile/from/profile/{username}关键字无效,laravel,Laravel,我有一条路线 Route::get('/{username}', 'ProfileController@profile')->name('profile.view'); 如果我把它放在文件中间,所有的路径都不能工作。 如果我把这个放在底部,那么一切都会正常 此外,如果我添加任何类似于Profile的工作,它也可以工作 Route::get('profile/{username}', 'ProfileController@profile')->name('profile.view')

我有一条路线

Route::get('/{username}', 'ProfileController@profile')->name('profile.view');

如果我把它放在文件中间,所有的路径都不能工作。 如果我把这个放在底部,那么一切都会正常

此外,如果我添加任何类似于Profile的工作,它也可以工作

Route::get('profile/{username}', 'ProfileController@profile')->name('profile.view');

如何解决这个问题?

这就是它的工作方式,因为您使用通配符匹配所有内容。因此,要么你把它放在文件的底部,它将被用作回退路径,这意味着上面的任何内容都不应该匹配,然后它将回退到该路径。或者,您可以使用正则表达式将用户名与其他路由不同,例如:

Route::get('{username}', 'ProfileController@profile')
    ->name('profile.view')
    ->where('username', 'YOUR REGEX HERE');
我会选择你展示的已经有效的:

Route::get('profile/{username}', 'ProfileController@profile')
    ->name('profile.view');

// or
Route::get('user/{username}', 'ProfileController@profile')
    ->name('profile.view');

我可以用If子句来验证是否没有{username}然后跳过它吗?我不这么认为,反正路由文件中没有。如果控制器与用户不匹配,它将抛出404。因此,路由模型绑定将为您做到这一点。