gpt4 book ai didi

php - Laravel 5.1 错误的 Illuminate\Encryption\Encrypter 实例

转载 作者:行者123 更新时间:2023-12-04 16:54:40 29 4
gpt4 key购买 nike

我有一个类,我试图用依赖注入(inject)替换 Crypt::encrypt 的外观用法:

       <?php namespace App\Libraries;

use Illuminate\Encryption\Encrypter;

class MyClass
{

public function __construct(Encrypter $encrypter)
{
$this->encrypter= $encrypter;
}

public function myMethod()
{
$this->encrypter->crypt('somevalue');
}

}

错误的实例正在被实例化:

Argument 1 passed to App\Libraries\MyClass::__construct() must be an instance of Illuminate\Encryption\Encrypter, instance of Illuminate\Encryption\McryptEncrypter given



外观解决没有问题,但我想了解为什么 DI 失败。
use Illuminate\Support\Facades\Crypt;
Crypt::encrypt('somevalue');

任何帮助表示赞赏。

最佳答案

因为你已经注册了McryptEncrypter而不是 Encrypter .
可能是您的密码或 key 不受支持。
更多信息请查看 Illuminate\Encryption\EncryptionServiceProvider .

    use Illuminate\Contracts\Encryption\Encrypter;

class MyClass
{

public function __construct(Encrypter $encrypter)
{
$this->encrypter= $encrypter;
}

public function myMethod()
{
$this->encrypter->crypt('somevalue');
}

}

另外,请关注 SOLI D 原理 Depend upon Abstractions. Do not depend upon concretions.替换 Encrypter 对其接口(interface)的具体实现。

The facade resolves without an issue but I'd like to understand why DI is failing.



Facade 有效,因为他只是解析您注册的内容,在您的情况下是 McryptEncrypter
希望它会有所帮助。

关于php - Laravel 5.1 错误的 Illuminate\Encryption\Encrypter 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34417018/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com