我想在我的ImageManager.php的services.yaml中获取一个参数,但是它不起作用,而且我无法解决此错误。

这是错误:


类型错误:函数App \ Manager \ ImageManager :: __ construct()的参数太少,在第14行的C:\ wamp64 \ www \ SymfonyAPI \ var \ cache \ dev \ ContainerZxFSS5S \ getImageManagerService.php中传递了0,并且恰好期望1


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%'


如果对于autowire&autoconfigure我说对了,我有这个错误:


RuntimeException
无法自动装配服务“ App \ Manager \ ImageManager”:方法“ __construct()”的参数“ $ targetDir”的类型为“ App \ Manager \ targetDir”,但未找到此类。


ImageManager.php

private $targetDir;

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




全面服务.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' }

最佳答案

您正在添加targetDir类型,并且您的应用程序认为这是某种类,您可以看到在错误has type "App\Manager\targetDir" but this class was not found.中,如果您使用的是php 7,则只需将targetDir替换为string将任何内容都设置为类型,即使您由于以下原因而自动装配true / false也会起作用:

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

关于symfony - Symfony4-函数App\Manager\ImageManager::__ construct()的参数太少,传递了0个且恰好期望1个,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48964262/

10-10 21:12