我已经安装了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' }
答案 0 :(得分:4)
这与错误的“或”部分有关。您可以像这样使用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