Heim  >  Fragen und Antworten  >  Hauptteil

Unauflösbare Abhängigkeiten (Laravel 8)

Bei Verwendung des Pakets „jasny/sso“ erhalte ich die folgende Fehlermeldung:

IlluminateContractsContainerBindingResolutionException
Unresolvable dependency resolving [Parameter #0 [ <required> callable $getBrokerInfo ]] in class JasnySSOServerServer

JasnySSOServerServer.php Intern:

/**
 * Class constructor.
 *
 * @phpstan-param callable(string):?array{secret:string,domains:string[]} $getBrokerInfo
 * @phpstan-param CacheInterface                                          $cache
 */
public function __construct(callable $getBrokerInfo, CacheInterface $cache)
{
    $this->getBrokerInfo = Closure::fromCallable($getBrokerInfo);
    $this->cache = $cache;

    $this->logger = new NullLogger();
    $this->session = new GlobalSession();
}

Ich habe es auch versucht:

php artisan route:clear
composer dump-autoload    
php artisan optimize:clear

Kann hier jemand auf das Problem hinweisen?

P粉729198207P粉729198207285 Tage vor400

Antworte allen(1)Ich werde antworten

  • P粉458913655

    P粉4589136552023-12-14 00:13:30

    由于 jasny/sso 不是 Laravel 包,因此如果没有基于其构造函数的一组关于如何实例化它的特定说明,则不应将其注册到容器中。

    AppServiceProviderregister()方法中添加以下代码:

    $this->app->bind(\Jasny\SSO\Server\Server::class, function($app) {
       $myCallable = function() {
           // do what you gotta do..
       };
    
       return new \Jasny\SSO\Server\Server($myCallable, $app->make(CacheInterface::class));
    });
    

    从那里您可以在应用程序中的任何位置执行以下操作:

    /** @var \Jasny\SSO\Server\Server $jasnyServer **/
    $jasnyServer = app()->make(\Jasny\SSO\Server\Server::class);
    $jasnyServer->changeTheWorld(true);
    

    它会自动使用我们在绑定中设置的可调用对象和 CacheInterface 填充构造函数(如果您只需要一个实例,也可以使用 $app->singleton() 而不是绑定)此类在整个脚本执行过程中都存在)。


    通常,您注册到容器中的任何内容都会受到 Laravel 的依赖注入的影响,因此您不能在构造函数中使用未知类型,因为 Laravel 无法知道 callable 是什么,并且会发生这种情况时会产生此错误。

    通常,如果您可以控制这一点,您可以从构造函数中删除可调用函数,并在类上使用 setter。

    private $callableFunc = null;
    
    public function setCallable(callable $func) : void
    {
        $this->callableFunc = $func;
    }
    

    Antwort
    0
  • StornierenAntwort