Project

General

Profile

Actions

Bug #93858

closed

dependency injection for middelware does not work

Added by Dieter Porth about 3 years ago. Updated almost 2 years ago.

Status:
Closed
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.

Actions #1

Updated by Oliver Bartsch about 3 years ago

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

Actions #2

Updated by Benjamin Franzke about 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.

Actions #3

Updated by Jarvis H almost 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/*" 

Actions #4

Updated by Benjamin Franzke over 2 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/*'

Actions #5

Updated by Christian Kuhn about 2 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.

Actions #6

Updated by razieh zivaralam almost 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

Actions

Also available in: Atom PDF