【问题标题】:(Symfony 4) Unable to access a class installed in my vendor folder(Symfony 4) 无法访问安装在我的供应商文件夹中的类
【发布时间】:2019-02-24 16:48:07
【问题描述】:

我已经安装了 Liip 包,我需要的类在我的容器中很明显可用,因为这是我的 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.

我想我需要知道如何公开?

这是我的 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' }

【问题讨论】:

  • 您的 IDE 似乎无法识别类的命名空间。我说的是红色字母和黄色突出显示。确保您已运行 composer install 并且您已正确设置自动加载文件。
  • 该服务很可能不是公开的,因此无法从容器中检索。这是默认行为,以阻止人们过度使用 ContainerAware,因为它被认为是一种不好的做法。您可以通过将服务名称添加到debug:container 或使用debug:autowiring 来检查(我认为)。

标签: symfony directory vendor


【解决方案1】:

这是关于您的错误的“或”部分。你可以像这样使用 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

【讨论】:

  • 您好 Domagoj,感谢您的回复。你知道指定参数的语法吗?我去了symfony.com/doc/current/controller/service.html,它没有给出将请求指定为服务参数的示例。
  • 您无需担心。 Symfony 知道该怎么做。您只需用controller.service_arguments 标记您的控制器。我还为_defaults 配置添加了部分。将自动装配和自动配置选项设置为 true
  • 当您使用 Symfony 4 控制器设置项目时,应该已经有标签并允许这样做。或者,您可以通过构造函数注入将服务注入控制器,或者,如果您使用 AbstractController,则覆盖 public static getSubscribedServices-array 以使服务可用。为此,只需检查 AbstractController + ControllerTrait,您可以在其中看到它是如何为 twig 等默认服务完成的。
  • 嗨 Domagoj,这似乎有效。我还必须将此添加到我的 services.yaml 文件中: Liip\ImagineBundle\Service\FilterService: '@liip_imagine.service.filter'
  • 我正要告诉你。很高兴它有效。 :)
猜你喜欢
  • 1970-01-01
  • 2019-09-18
  • 2015-09-30
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
  • 2016-06-11
  • 2013-09-17
  • 1970-01-01
相关资源
最近更新 更多