typo3dev
typo3dev

Reputation: 107

TYPO3: Inject Service Classes into AuthServiceClass with Constructor

I'm using TYPO3 10.2 and trying to inject some Service Classes that I created into my authentication service.

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

The Constructor in 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;
    }

I am getting the following error:

Too few arguments to function Vendor\MyExt\Service\AuthService::__construct(), 0 passed in C:\xampp\htdocs\myproject\typo3\sysext\core\Classes\Utility\GeneralUtility.php on line 3461 and exactly 3 expected

Any advice what's going on here?

I used the same Constructor in my ControllerClass and everything is working fine there.

Thanks so far!

Upvotes: 2

Views: 1303

Answers (1)

Daniel
Daniel

Reputation: 7016

Looks like your AuthenticationService is internally instantiated by GeneralUtility::makeInstance(). This is true for many classes that you register at some point and TYPO3 then takes care of the creation of the class (think of user functions, plugin controller, module controller, authentication services, hooks, etc).

GeneralUtility::makeInstance() needs to get the class out of the DI container for the DI to work but this is only possible for classes made public during container compilation.

For that reason the solution to your problem should be to declare the class AuthService as public in your Configuration/Services.yaml:

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

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

  Vendor\MyExt\Service\AuthService:
    public: true

You can find this explained in the official docs or in my blog post about that topic.

Upvotes: 2

Related Questions