在核心 php 中,我们使用 url_encode()
和 url_decode()
这样我们就可以在 laravel 4 中使用这个函数。如果可能的话,请告诉我怎么做。
<p> <a href="userregistrations/{{ $users-> id }}">{{ $users-> username }}</a>
我想编码$users->id
。此 ID 编码此 ID 的过程或方法是什么,请帮助解码。
最佳答案
Laravel 提供了一个名为 encrypt 的辅助函数。所有加密值均使用 OpenSSL 和 AES-256-CBC 密码进行加密。下面是一个如何使用它的例子。
public function storeSecret(Request $request, $id)
{
$user = User::findOrFail($id);
$user->fill([
'secret' => encrypt($request->secret)
])->save();
}
加密值在加密期间通过序列化传递,这允许对对象和数组进行加密。因此,接收加密值的非 PHP 客户端将需要反序列化数据。如果您想在不进行序列化的情况下加密和解密值,您可以使用 Crypt 门面的 encryptString 和 decryptString 方法:
use Illuminate\Support\Facades\Crypt;
$encrypted = Crypt::encryptString('Hello world.');
$decrypted = Crypt::decryptString($encrypted);
您可以使用 decrypt helper 解密值。如果该值不能被正确解密,比如当 MAC 无效时,将抛出 Illuminate\Contracts\Encryption\DecryptException:
use Illuminate\Contracts\Encryption\DecryptException;
try {
$decrypted = decrypt($encryptedValue);
} catch (DecryptException $e) {
//
}
关于php - 如何在 laravel 中使用编码和解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24734536/