Bug #93858

dependency injection for middelware does not work

Added by Dieter Porth 7 months ago. Updated 4 months ago.

Status:
New
Priority:
Should have
Assignee:
-
Category:
-
Target version:
-
Start date:
2021-04-04
Due date:
% Done:

0%

Estimated time:
TYPO3 Version:
10
PHP Version:
7.4
Tags:
Complexity:
Is Regression:
Sprint Focus:

Description

If i use my middleware without dependency-injection. it works fine. But I needed a responsefactory and tried the code for teh dependency-Injectioon, which is described in the documentation. It wont work.
https://docs.typo3.org/m/typo3/reference-coreapi/10.4/en-us/ApiOverview/RequestHandling/Index.html

I tried the other example in the dokumentation and got the following exception:

(1/1) ArgumentCountError

Too few arguments to function Porthd\Simpledataedit\Middleware\UpdateEditing::__construct(), 0 passed in /var/www/html/web/typo3/sysext/core/Classes/Utility/GeneralUtility.php on line 3443 and exactly 3 expected
in /var/www/html/web/typo3conf/ext/simpledataedit/Classes/Middleware/UpdateEditing.php line 36

    /** @var ClientInterface */
    private $client;

    public function __construct(
        ResponseFactoryInterface $responseFactory,
        RequestFactoryInterface $requestFactory,
        ClientInterface $client
    ) {

at Porthd\Simpledataedit\Middleware\UpdateEditing->__construct()
in /var/www/html/web/typo3/sysext/core/Classes/Utility/GeneralUtility.php line 3443

            return self::$container->get($className);
        }

        // Create new instance and call constructor with parameters
        $instance = new $finalClassName(...$constructorArguments);
        // Register new singleton instance, but only if it is not a known PSR-11 container service
        if ($instance instanceof SingletonInterface && !(self::$container !== null && self::$container->has($className))) {
            self::$singletonInstances[$finalClassName] = $instance;
        }

at TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Porthd\\Simpledataedit\\Middleware\\UpdateEditing')
in /var/www/html/web/typo3/sysext/core/Classes/Http/MiddlewareDispatcher.php line 166

It seems, that the dependency injection does not work, because the makeInstace don't get any constructorArguments.

            public function handle(ServerRequestInterface $request): ResponseInterface
            {
                if ($this->container !== null && $this->container->has($this->middleware)) {
                    $middleware = $this->container->get($this->middleware);
                } else {
                    $middleware = GeneralUtility::makeInstance($this->middleware); /* line 166 */
                }

                if (!$middleware instanceof MiddlewareInterface) {
                    throw new \InvalidArgumentException(get_class($middleware) . ' does not implement ' . MiddlewareInterface::class, 1516821342);
                }
                return $middleware->process($request, $this->next);
            }

Did I something wrong? Or is it really a bug?

P.S. I will currently ignore the dependency-injection in my middleware and instanciate directly the responseFactory.

#1

Updated by Oliver Bartsch 7 months ago

Have you cleared all caches via the install tool? Maintenance > Flush cache

#2

Updated by Benjamin Franzke 7 months ago

Did you setup a dependency injection configuration in Configuration/Services.yaml as described in
https://docs.typo3.org/m/typo3/reference-coreapi/10.4/en-us/ApiOverview/DependencyInjection/Index.html#configure-dependency-injection-in-extensions

If so, please post the contents of your extensions Configuration/Services.yaml file.

#3

Updated by Jarvis H 4 months ago

I have the same issue in Typo3 11.2, clearing cache does not help.

Constructor-injection works fine in other classes, just not in middleware.

services.yaml:

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

  VendorHere\ExtensionKeyHere\:
    resource: "../Classes/*" 
    exclude: "../Classes/Domain/Model/*" 

Also available in: Atom PDF