(Symfony 4) 无法访问安装在我的供应商文件夹中的 class

(Symfony 4) Unable to access a class installed in my vendor folder

我已经安装了 Liip 包,我需要的 class 显然在我的容器中可用,因为这是我的 debug:container 命令的结果:

$ bin/console debug:container
liip_imagine.service.filter       Liip\ImagineBundle\Service\FilterService

只是为了向您展示它就在那里,这是我的文件夹结构的图片:

这是我用来在我的控制器中访问它的代码:

public function saveProfileEditAction(Request $request)
{
    $user = $this->getUser();
    $imagine = $this
        ->container
        ->get('liip_imagine.service.filter');

这是我得到的错误:

The "liip_imagine.service.filter" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.

我想我需要知道怎么做 public?

这是我的 yaml 文件的样子:

liip_imagine :
    # configure resolvers
    resolvers :
        # setup the default resolver
        default :
            # use the default web path
            web_path : ~
    # your filter sets are defined here
    filter_sets :
        # use the default cache configuration
        cache : ~
        # the name of the "filter set"
        my_thumb :
            # adjust the image quality to 75%
            quality : 75
            # list of transformations to apply (the "filters")
            filters :
                # create a thumbnail: set size to 120x90 and use the "outbound" mode
                # to crop the image when the size ratio of the input differs
                thumbnail  : { size : [120, 90], mode : outbound }
                thumb_square :  { size : [300, 300], mode : outbound }
                thumb_rectangle_md : { size : [670, 400], mode : outbound }
                thumb_hd : { size : [1920, 1080], mode : outbound }
                # create a 2px black border: center the thumbnail on a black background
                # 4px larger to create a 2px border around the final image
                background : { size : [124, 94], position : center, color : '#000000' }

这是关于您的错误的 'or' 部分。你可以像这样使用 Symfony 的依赖注入:

在控制器中:

public function saveProfileEditAction(Request $request, FilterService $imagine) // Typehint service to controller method (remember to `use` on the top of the file)
{
    $user = $this->getUser();
    $imagine->...; // Use it

在 services.yml 中将您的控制器注册为服务并标记它,以便 symfony 知道它需要为它注入服务。

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    YourNamespace/*Bundle/Controller/YourController: // Change this
        tags: [ 'controller.service_arguments' ]

https://symfony.com/doc/current/service_container.html#service-container-services-load-example