Unresolvable dependencies (Laravel 8)
P粉729198207
P粉729198207 2023-12-13 10:13:44

Using the "jasny/sso" package, I get the following error:

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

JasnySSOServerServer.php Internal:

/**
 * 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();
}

I also tried:

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

Can anyone point out the problem here?

P粉729198207
P粉729198207

reply all(1)
P粉458913655

Since jasny/sso is not a Laravel package, it should not be registered with the container without a specific set of instructions on how to instantiate it based on its constructor.

Add the following code in the register() method of AppServiceProvider:

$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));
});

From there you can do the following from anywhere in the application:

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

It will automatically populate the constructor with the callable and CacheInterface we set in the binding (if you only need a single instance, you can also use $app->singleton() instead of the binding ) This class exists throughout the execution of the script).


Generally anything you register into the container will be affected by Laravel's dependency injection, so you can't use unknown types in the constructor because Laravel has no way of knowing what the callable is and that will happen This error occurs when this is the case.

Typically, if you have control over this, you can remove the callable function from the constructor and use a setter on the class.

private $callableFunc = null;

public function setCallable(callable $func) : void
{
    $this->callableFunc = $func;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!