使用“jasny/sso”包,我收到以下錯誤:
IlluminateContractsContainerBindingResolutionException Unresolvable dependency resolving [Parameter #0 [ <required> callable $getBrokerInfo ]] in class JasnySSOServerServer
JasnySSOServerServer.php 內部:
/** * 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(); }
我也嘗試過:
php artisan route:clear composer dump-autoload php artisan optimize:clear
有人能指出這里的問題嗎?
由于 jasny/sso 不是 Laravel 包,因此如果沒有基于其構造函數(shù)的一組關于如何實例化它的特定說明,則不應將其注冊到容器中。
在AppServiceProvider
的register()
方法中添加以下代碼:
$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)); });
從那里您可以在應用程序中的任何位置執(zhí)行以下操作:
/** @var \Jasny\SSO\Server\Server $jasnyServer **/ $jasnyServer = app()->make(\Jasny\SSO\Server\Server::class); $jasnyServer->changeTheWorld(true);
它會自動使用我們在綁定中設置的可調用對象和 CacheInterface 填充構造函數(shù)(如果您只需要一個實例,也可以使用 $app->singleton()
而不是綁定)此類在整個腳本執(zhí)行過程中都存在)。
通常,您注冊到容器中的任何內容都會受到 Laravel 的依賴注入的影響,因此您不能在構造函數(shù)中使用未知類型,因為 Laravel 無法知道 callable
是什么,并且會發(fā)生這種情況時會產生此錯誤。
通常,如果您可以控制這一點,您可以從構造函數(shù)中刪除可調用函數(shù),并在類上使用 setter。
private $callableFunc = null; public function setCallable(callable $func) : void { $this->callableFunc = $func; }