Bạn có thể dùng base64_encode hoặc use Illuminate\Support\Facades\Crypt; hoặc dùng helper $encryptedId = encrypt($id);
Trong controller hoặc view
use Illuminate\Support\Facades\Crypt;
// Mã hóa ID
$encryptedId = Crypt::encryptString($id); // $id là số nguyên
$url = route('users.show', ['id' => $encryptedId]);
Giải mã tham số
use Illuminate\Support\Facades\Crypt;
public function show($id)
{
try {
$decryptedId = Crypt::decryptString($id); // Giải mã
$user = User::findOrFail($decryptedId);
return view('users.show', compact('user'));
} catch (\Illuminate\Contracts\Encryption\DecryptException $e) {
abort(404); // hoặc redirect với thông báo lỗi
}
}
https://tritue.edu.vn/tuecode/tracnghiem30/index.php/baiviet/post/view/id/237?id=237