(Symfony 4)无法访问我的vendor文件夹中安装的类

问题描述 投票:0回答:1

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

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

只是为了告诉你它在那里,这是我的文件夹结构的图片:

enter image description here

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

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' }
symfony directory vendor
1个回答
3
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.