Laravel 如何根据当前登录的用户自定义用户配置文件 url
Laravel how to customize user profile url based on currently logged in user
我创建了一个 IndexController.php class 来处理与用户相关的数据控制。
所以我可以通过以下方法获取当前登录的用户。但是 url 有点通用 url。我希望它是基于登录用户的自定义 url。例如,如果 userx 以用户身份登录,他的个人资料应显示为 http://127.0.0.1:8000/user/userx。我该如何解决这个问题?
IndexController.php
class IndexController extends Controller{
public function Index(){
return view('dashboard.index');
}
public function userLogout(){
Auth::logout();
return Redirect()->route('login');
}
public function userProfile(){
$id = Auth::user()->id;
$user = User::find($id);
return view('dashboard.profile',compact('user'));
}
}
Web.php
Route::get('/user/profile',[IndexController::class, 'userProfile'])->name('user.profile');
使用参数创建路由,然后使用该变量获取配置文件:
Route::get('/user/profile/{username}',[IndexController::class, 'userProfile'])->name('user.profile');
然后在您的控制器中,您应该可以在方法参数中访问此用户名:
public function userProfile(Request $request, $username)
{
$user = User::query()->where('username', $username)->firstOrFail();
// ...
}
如果您想在 自己的 个人资料页面上添加额外的功能(例如更改密码等),则必须检查经过身份验证的用户 Auth::user()
。或者,您可以保留默认 /user/profile
路由作为 URL 以在您想要更改自己的配置文件时访问。有多种方法可以解决这个问题:)
你需要检查两个Laravel概念:
路线
Route::get('/user/{profile}',[IndexController::class, 'userProfile'])->name('user.profile');
控制器
public function userProfile(User $profile){
// Check if $profile is the same as Auth::user()
// If only the user can see his own profile
$id = Auth::user()->id;
$user = User::find($id);
return view('dashboard.profile',compact('user'));
}
Blade
<a href="{{ route('user.profile', ["profile" => $user->id]) }}"> See profile </a>
或者
<a href="{{ route('user.profile', ["profile" => Auth::user()->id]) }}"> See profile </a>
我创建了一个 IndexController.php class 来处理与用户相关的数据控制。
所以我可以通过以下方法获取当前登录的用户。但是 url 有点通用 url。我希望它是基于登录用户的自定义 url。例如,如果 userx 以用户身份登录,他的个人资料应显示为 http://127.0.0.1:8000/user/userx。我该如何解决这个问题?
IndexController.php
class IndexController extends Controller{
public function Index(){
return view('dashboard.index');
}
public function userLogout(){
Auth::logout();
return Redirect()->route('login');
}
public function userProfile(){
$id = Auth::user()->id;
$user = User::find($id);
return view('dashboard.profile',compact('user'));
}
}
Web.php
Route::get('/user/profile',[IndexController::class, 'userProfile'])->name('user.profile');
使用参数创建路由,然后使用该变量获取配置文件:
Route::get('/user/profile/{username}',[IndexController::class, 'userProfile'])->name('user.profile');
然后在您的控制器中,您应该可以在方法参数中访问此用户名:
public function userProfile(Request $request, $username)
{
$user = User::query()->where('username', $username)->firstOrFail();
// ...
}
如果您想在 自己的 个人资料页面上添加额外的功能(例如更改密码等),则必须检查经过身份验证的用户 Auth::user()
。或者,您可以保留默认 /user/profile
路由作为 URL 以在您想要更改自己的配置文件时访问。有多种方法可以解决这个问题:)
你需要检查两个Laravel概念:
路线
Route::get('/user/{profile}',[IndexController::class, 'userProfile'])->name('user.profile');
控制器
public function userProfile(User $profile){
// Check if $profile is the same as Auth::user()
// If only the user can see his own profile
$id = Auth::user()->id;
$user = User::find($id);
return view('dashboard.profile',compact('user'));
}
Blade
<a href="{{ route('user.profile', ["profile" => $user->id]) }}"> See profile </a>
或者
<a href="{{ route('user.profile', ["profile" => Auth::user()->id]) }}"> See profile </a>