Bug #93858
closeddependency injection for middelware does not work
0%
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.
Updated by Oliver Bartsch over 3 years ago
Have you cleared all caches via the install tool? Maintenance > Flush cache
Updated by Benjamin Franzke over 3 years 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.
Updated by Jarvis H over 3 years 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/*"
Updated by Benjamin Franzke almost 3 years ago
- Status changed from New to Needs Feedback
The error is pretty clear: the middleware has not been correctly registered in DI.
Please post you exact Services.yaml
and the full Middleware class.
Configuration/Services.yaml
(with capital "S") should read like:
services: _defaults: autowire: true autoconfigure: true public: false Porthd\Simpledataedit\: resource: '../Classes/*' exclude: '../Classes/Domain/Model/*'
Updated by Christian Kuhn almost 3 years ago
- Status changed from Needs Feedback to Closed
I'm pretty certain that's a configuration issue in the extension as Benjamin mentioned already. I hope it's ok to close the issue for now. Please keep us in the loop in case the problem persists, maybe by asking in the general slack channels for additional help.
Updated by razieh zivaralam over 2 years ago
hi
i've changed the services.yaml file
services:
_defaults:
autowire: true
autoconfigure: true
public: false
Ol\He\:
resource: '../Classes/*'
exclude: '../Classes/Domain/Model/*'
but the error dosen't change anymore
Too few arguments to function Ol\He\Middleware\TestMiddleware::__construct(), 0 passed in /html/typo3-razz/typo3_src-10.4.21/typo3/sysext/core/Classes/Utility/GeneralUtility.php on line 3477 and exactly 1 expected