1 回答
TA贡献1829条经验 获得超4个赞
您可以制作一个中间件来检查经过身份验证的用户 id 是否等于方法$request->document->user_id中的handle,并在控制器的构造方法中应用中间件(显示、查看、删除)
这是一个示例实现
php artisan make:middleware DocumentsOwnerShip
<?php
namespace App\Http\Middleware;
use Closure;
class DocumentsOwnerShip
{
public function handle($request, Closure $next)
{
if ($request->document->user_id !== auth()->id()) {
return redirect('documents');
}
return $next($request);
}
}
在你的控制器中
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Middleware\DocumentsOwnerShip;
class DocumentsController extends Controller
{
public function __construct()
{
$this->middleware(DocumentsOwnerShip::class)->only(['view', 'show', 'delete']);
}
public function show(Document $document)
{
return view('documents.show', compact('document'));
}
}
- 1 回答
- 0 关注
- 133 浏览
添加回答
举报