Symfony4 - Too few arguments to function App\Manager\ImageManager::__construct(), 0 passed and exactly 1 expected

14,057

Solution 1

You are adding a type of targetDir and your Application thinks that is some kind of class and you can see that in your error has type "App\Manager\targetDir" but this class was not found., just replace targetDir with string if you are on php 7 or don't put anything as a type and it will work even if you have autowire true/false because of:

App\Manager\ImageManager:
    arguments:
        $targetDir: '%images_directory%'

Solution 2

To clarify the answer by @kunicmarko20

Your service constructor requires an object of App\Manager\targetDir as the $targetDir argument, but you are supplying your service with a string as the $targetDir argument.

You need to change your service constructor to look like one of the following.

PHP 7.x

public function __construct(string $targetDir)

PHP 5.x

public function __construct($targetDir) 

Update with configuration changes

The second issue you have is that you have prototyping enabled on your service class directory. This causes Symfony to override your manual service definition with the prototype service definition.

So what happens is your manual service definition is created, then overridden by the auto configured definition.

The prototype definition is

App\:
    resource: '../src/*'
    exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'

Since it is below your manual service definition, Symfony uses it instead of your manual definition.

For example if I write

services:
   AppBundle\MyDirectory\Object:
      parameters: ['a']

   AppBundle\MyDirectory\Object:
      parameters: ['b']

The end result of what symfony uses as the service definition would be.

new AppBundle\MyDirectory\Object('b');

You should change your services.yml to the following:

parameters:
    liip_imagine.mozjpeg.binary: /mozjpeg/cjpeg.exe
    images_directory: '%kernel.project_dir%/public/uploads/images/'
    mozjpg_directory: '%kernel.project_dir%/mozjpg'
    locale: 'en'

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

    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'

    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

    #... Your manual service definitions below here.

    App\Manager\ImageManager:
        arguments:
            $targetDir: '%images_directory%'

    App\EventListener\ImageUploadListener:
        tags:
            - { name: doctrine.event_listener, event: prePersist }
            - { name: doctrine.event_listener, event: preUpdate } 

    app.post_processor.my_custom_post_processor:
        class: '%kernel.project_dir%/src/Controller/ImageController.php'
        tags:
            - { name: 'liip_imagine.filter.post_processor', post_processor: 'mozjpeg' }
Share:
14,057

Related videos on Youtube

Morgan Millet
Author by

Morgan Millet

Updated on June 04, 2022

Comments

  • Morgan Millet
    Morgan Millet almost 2 years

    I tring to get an argument in my services.yaml for my ImageManager.php but it's not working and I can not solve this error.

    here is the mistake :

    Type error: Too few arguments to function App\Manager\ImageManager::__construct(), 0 passed in C:\wamp64\www\SymfonyAPI\var\cache\dev\ContainerZxFSS5S\getImageManagerService.php on line 14 and exactly 1 expected

    services.yaml

    parameters:
        images_directory: '%kernel.project_dir%/public/uploads/images/'
    ...
    
    services:
        _defaults:
            autowire: false
            autoconfigure: false
            public: true
    
    App\Manager\ImageManager:
        arguments:
            $targetDir: '%images_directory%'
    

    if for autowire & autoconfigure I put true I have this error :

    RuntimeException Cannot autowire service "App\Manager\ImageManager": argument "$targetDir" of method "__construct()" has type "App\Manager\targetDir" but this class was not found.

    ImageManager.php

    private $targetDir;
    
    public function __construct(targetDir $targetDir) 
    {
        $this->targetDir = $targetDir;
    }
    

    Full services.yml

    parameters:
        liip_imagine.mozjpeg.binary: /mozjpeg/cjpeg.exe
        images_directory: '%kernel.project_dir%/public/uploads/images/'
        mozjpg_directory: '%kernel.project_dir%/mozjpg'
        locale: 'en'
    
    services:
        _defaults:
            autowire: false
            autoconfigure: false
            public: true
    
    App\Manager\ImageManager:
        arguments:
            $targetDir: '%images_directory%'
    
    App\EventListener\ImageUploadListener:
        tags:
            - { name: doctrine.event_listener, event: prePersist }
            - { name: doctrine.event_listener, event: preUpdate } 
    
    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
    
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']
    
    app.post_processor.my_custom_post_processor:
        class: '%kernel.project_dir%/src/Controller/ImageController.php'
        tags:
            - { name: 'liip_imagine.filter.post_processor', post_processor: 'mozjpeg' }
    
    • Morgan Millet
      Morgan Millet about 6 years
      I don't understand how, but I just clone my project that was on github and everythings works. I'm sorry for making you waste your time, thanks anyway.
  • Morgan Millet
    Morgan Millet about 6 years
    Thank you for your answer, but it does not change anything, I always have the same error. (I have php 7.x)
  • Will B.
    Will B. about 6 years
    @MorganMillet Be sure that you are clearing your symfony cache.
  • Will B.
    Will B. about 6 years
    @MorganMillet how are you retrieving the service or what are you doing to produce the stated error message(s)?
  • Morgan Millet
    Morgan Millet about 6 years
    I called the service from a function in my controller : $imageManager = $this->container->get(ImageManager::class); I use Postman because I develop a REST API, so for exemple I send a POST request to this url : 127.0.0.1:8000/images (My app works without this error) The problem should come from the services.yaml file but my code is exactly like the doc : symfony.com/doc/current/…
  • Will B.
    Will B. about 6 years
    The only thing I can think of is that you may have a prototyping configured for a resource that your service class is contained in, Which is overriding your manual service definition. That would explain why the arguments supplied are 0, and why autowire attempted to supply one. Can you post your entire services.yml and services_dev.yml and obfuscate confidential values (like passwords and email addresses)?
  • Will B.
    Will B. about 6 years
    @MorganMillet updated the answer with the configuration changes.