laravel - 从 Route Web.php 的 /profile/{username} 关键字中删除 /profile/ 不起作用
问题描述
我有一条路线
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');
推荐阅读
- r - 抽样随机行
- javascript - 无法在 Expo React 本机应用程序中清除间隔
- github - 我粘贴了用于克隆的 Github git URL,但得到:无法解析主机:github.com
- reactjs - 使用 useEffect 在页面加载时从上下文中获取数据
- visual-studio - 重置视觉工作室按钮
- vue.js - 无法在 Vuejs 中验证道具
- javascript - Mongoose/Mongodb 通过与另一个数组比较查找嵌套数组
- anylogic - 在 Anylogic 中灵活更改模拟持续时间
- reactjs - React Scroll - 在 NavBar 中使用 React Scroll to 滚动到特定组件
- azure - Azure B2C - 2 个应用程序,不同的协议