簡體   English   中英

TYPO3:使用構造函數將服務類注入到 AuthServiceClass 中

[英]TYPO3: Inject Service Classes into AuthServiceClass with Constructor

我正在使用 TYPO3 10.2 並嘗試將我創建的一些服務類注入到我的身份驗證服務中。

class AuthService extends \TYPO3\CMS\Core\Authentication\AuthenticationService

AuthService 中的構造函數:

    /**
     * Contains the configuration of the current extension
     * @var ConfigurationService
     */
    protected $configurationService;

    /**
     * @var RestClientService
     */
    protected $restClientService;

    /**
     * @var ConnectionPool
     */
    protected $connectionPool;

    /**
     * 
     * @param ConfigurationService $configurationService
     * @param RestClientService $restClientService
     * @param ConnectionPool $connectionPool
     */
    public function __construct(ConfigurationService $configurationService, RestClientService $restClientService, ConnectionPool $connectionPool)
    {
        $this->configurationService = $configurationService;

        $this->restClientService = $restClientService;

        $this->connectionPool = $connectionPool;
    }

我收到以下錯誤:

函數 Vendor\\MyExt\\Service\\AuthService::__construct() 的參數太少,0 傳入 C:\\xampp\\htdocs\\myproject\\typo3\\sysext\\core\\Classes\\Utility\\GeneralUtility.php 在第 3461 行,正好是 3 個預期

任何建議這里發生了什么?

我在我的 ControllerClass 中使用了相同的構造函數,那里一切正常。

到目前為止,謝謝!

看起來您的AuthenticationService是由GeneralUtility::makeInstance()內部實例化的。 這對於您在某個時候注冊的許多類都是正確的,然后 TYPO3 負責創建類(想想用戶函數、插件控制器、模塊控制器、身份驗證服務、鈎子等)。

GeneralUtility::makeInstance()需要將類從 DI 容器中取出才能使 DI 工作,但這僅適用於在容器編譯期間public類。

出於這個原因,您的問題的解決方案應該是在您的Configuration/Services.yaml AuthServiceAuthService為 public :

services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

  Vendor\MyExt\:
    resource: '../Classes/*'

  Vendor\MyExt\Service\AuthService:
    public: true

您可以在官方文檔或我關於該主題的博客文章中找到對此的解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM