【发布时间】:2023-04-05 04:29:02
【问题描述】:
我有一个视图 (resources/view/front/auth/profile.blade.php),我在文件 web.php 中的路线是:
Route::get('/profile/{user}','UserController@edit')
->name('profile')
->middleware('profilecheck');
我的问题是当用户登录并被重定向到他们自己的个人资料页面 (http://exmaple.com/profile/2) 时,他/她可以将 URL 更改为 http://exmaple.com/profile/3 并查看其他用户的个人资料。
我想使用中间件来检查带有 URL 参数 {user} 的经过身份验证的用户 ID。 $user->id 将传递给 {user},但我不知道如何。
中间件UserProfile.php:
<?php
namespace App\Http\Middleware;
use App\User;
use Closure;
class UserProfile
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// $request->user()->id
// Auth::user()->id
return $next($request);
}
}
【问题讨论】:
-
fi 已记录,只需使用“/profile”作为 url,在控制器中使用
return view(...)->with('user', auth()->user()),或定义一些策略laravel.com/docs/5.8/authorization -
去掉在url中传递id的选项,直接通过Auth::user()访问用户,使用控制器中的auth门面返回当前用户
-
@Berto99,谢谢你,我只是按照你说的做了,而且成功了。