【问题标题】:Codeigniter 4: Undefined type 'App\Controllers\Encryption' with Encryption::createKey()Codeigniter 4:使用 Encryption::createKey() 的未定义类型“App\Controllers\Encryption”
【发布时间】:2021-03-20 20:54:05
【问题描述】:

我是 Codeigniter 4 框架的新手,我正在尝试使用 Encryption Service

我想使用代码生成一个用于加密的密钥以存储在app/Config/Encryption.php 文件中:

$key = Encryption::createKey();

我正在尝试创建一个密钥,因为文档说:

密钥应尽可能随机,不能是正则 文本字符串,也不是散列函数的输出等。创建一个 正确的密钥,您可以使用加密库的 createKey() 方法。

但是当我尝试上面链接中给出的代码时,VSCode 给我一个错误说: 未定义类型“App\Controllers\Encryption”。

Codeigniter 给了我错误:Class 'App\Controllers\Encryption' not found

如何使用 Encryption 库并解决上述错误并使用 Encryption::createKey() 生成密钥?

【问题讨论】:

    标签: php codeigniter encryption codeigniter-4


    【解决方案1】:

    您必须注意命名空间。使用完整的命名空间:

    $key = \CodeIgniter\Encryption\Encryption::createKey();
    

    或使用use 关键字首先“包含”该类:

    use CodeIgniter\Encryption\Encryption; // usually to be put at the top after the namespace `declaration`
    
    ...
    
    $key = Encryption::createKey();
    

    【讨论】:

    • 我尝试了两种解决方案。在分别和一起实施这两种解决方案后,我收到此错误:调用未定义的方法 Config\Encryption::createKey()。我错过了什么吗?
    • 嗨@HemantK。我从文档中复制粘贴了错误的命名空间。请查看更新后的我的代码。
    猜你喜欢
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 2020-10-05
    • 2019-06-11
    • 1970-01-01
    • 2021-02-18
    • 2020-06-27
    • 2017-06-30
    相关资源
    最近更新 更多