無法解決的依賴關係 (Laravel 8)
P粉729198207
P粉729198207 2023-12-13 10:13:44
0
1
402

使用“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

有人能指出這裡的問題嗎?

P粉729198207
P粉729198207

全部回覆(1)
P粉458913655

由於 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;
}
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!