【问题标题】:how to use encode and decode in laravel如何在 laravel 中使用编码和解码
【发布时间】:2014-09-04 06:02:21
【问题描述】:

在核心 php 中,我们使用 url_encode()url_decode(),所以我们可以在 laravel 4 中使用这个函数。如果可能,请告诉我如何。

<p> <a href="userregistrations/{{ $users-> id }}">{{ $users-> username }}</a>

我想编码$users-&gt;id。这个 id 是什么过程或方法来编码这个 id 请帮助解码。

【问题讨论】:

  • 我不知道 laravel ,但那里的代码似乎是由模板引擎解析的。也许您应该在将其传递给引擎之前进行编码,但我认为模板引擎会负责编码(至少应该如此,否则您应该使用 php 作为模板引擎)。
  • 试试{{{ $users-&gt;id }}}

标签: php laravel laravel-4 laravel-blade laravel-3


【解决方案1】:

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 门面的 encryptStringdecryptString 方法:

use Illuminate\Support\Facades\Crypt;

$encrypted = Crypt::encryptString('Hello world.');

$decrypted = Crypt::decryptString($encrypted);

您可以使用解密助手解密值。如果无法正确解密该值,例如 MAC 无效,则会抛出 Illuminate\Contracts\Encryption\DecryptException:

use Illuminate\Contracts\Encryption\DecryptException;

try {
    $decrypted = decrypt($encryptedValue);
} catch (DecryptException $e) {
    //
}

参考https://laravel.com/docs/5.4/encryption

【讨论】:

  • 编码和加密是两种不同的动物。我不确定线程​​所有者是否是这个意思。
猜你喜欢
  • 1970-01-01
  • 2019-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-25
  • 1970-01-01
  • 1970-01-01
  • 2012-07-04
相关资源
最近更新 更多