从Controller访问Liip Imagine捆绑包-将服务分配给变量(Symfony 4) [英] Access Liip Imagine bundle from Controller - assign service to variable (Symfony 4)

查看:53
本文介绍了从Controller访问Liip Imagine捆绑包-将服务分配给变量(Symfony 4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

中(Symfony 4)如何从PHP代码中访问Liip Imagine捆绑包?我找到了使用Liip Imagine Cache的解决方案,但无法正常工作.两种解决方案,都不适合我.我完全不知道为什么.

On (Symfony 4) How do I access the Liip Imagine bundle from within PHP code? I found a solution to use the Liip Imagine Cache but I can't get it to work. Two solutions, neither of them work for me. I have absolutely no Idea why.

当我尝试使用harmstyler的解决方案时

When I try harmstyler's solution with

$imagineCacheManager = $this->get('liip_imagine.cache.manager');

在我的控制器中,然后收到ServiceNotFoundException

in my Controller then I get a ServiceNotFoundException

服务"liip_imagine.cache.manager"未找到:即使它存在于应用程序的容器中,"App \ Controller \ MemberController"内部的容器也是一个较小的服务定位器,仅了解教义",表单".factory","http_kernel","parameter_bag","request_stack","router","security.authorization_checker","security.csrf.token_manager","security.token_storage","serializer","session"和"twig"服务.尝试改用依赖项注入.
Service "liip_imagine.cache.manager" not found: even though it exists in the app's container, the container inside "App\Controller\MemberController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session" and "twig" services. Try using dependency injection instead.

所以我尝试了Alister Bulman的建议,将其手动注入到service.yaml中的类中,但这也不起作用.

So I tried Alister Bulman's suggestion to inject it manually into a class in the service.yaml but that doesn't work either.

在我的service.yaml中,

In my service.yaml I have

app.imagine_cache_manager:
    class: Liip\ImagineBundle\Imagine\Cache\CacheManager
    arguments: ["@liip_imagine.cache.manager"]

在我的控制器中,

$imagineCacheManager = $this->get('app.imagine_cache_manager');

这导致我遇到相同的异常

which leads me to the same Exception

未找到服务"app.imagine_cache_manager":即使它存在于应用程序的容器中,"App \ Controller \ MemberController"内部的容器还是一个较小的服务定位器,仅知道"doctrine","form.factory","http_kernel","parameter_bag","request_stack","router","security.authorization_checker","security.csrf.token_manager","security.token_storage","serializer","session"和"twig"服务.尝试改用依赖项注入.

Service "app.imagine_cache_manager" not found: even though it exists in the app's container, the container inside "App\Controller\MemberController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session" and "twig" services. Try using dependency injection instead.

[顺便说一句,我实际上想做的是:我有成员,每个成员都有一张图像.创建成员我有一个图像上传,让Liip创建主图像的调整大小图像.当我删除图像或成员时,我当然也想通过Liip删除缓存的图像.这就是为什么我尝试让Liip缓存管理器能够获取缓存的图像路径以将其删除.另一种方法是使用事件监听器,但这对我来说都不起作用.我将在另一个问题中总结监听器方法.]

推荐答案

这是由于不赞成将 Controller 类用作Symfony4中的控制器基类.现在推荐的 AbstractController 类使用一个较小的容器,该容器仅通过 ServiceSubscriberInterface 声明了服务(您可以查看 AbstractController :: getSubscribedServices()方法以查看默认情况下可用的服务.

This is due to the deprecation of the Controller class as the base class of controllers in Symfony4. The now recommended AbstractController class uses a smaller container with only the declared services via the ServiceSubscriberInterface (you can take a look in the AbstractController::getSubscribedServices() method to see what services are available by default).

您可以:

在Controller中扩展 getSubscribedServices()函数,并将 CacheManager 作为服务之一.

Extend the getSubscribedServices() function in your Controller and include the CacheManager as one of the services.

直接在控制器中注入服务(推荐):

Inject the service directly in your controller (recommended):

namespace App\Controller;

use Liip\ImagineBundle\Imagine\Cache\CacheManager;

class MemberController extends AbstractController 
{
    public function __construct(CacheManager $liipCache)
    {
        $this->imagineCacheManager = $liipCache;
    }
}

您可以阅读有关此更改的信息

You can read about this change in the announcement

这篇关于从Controller访问Liip Imagine捆绑包-将服务分配给变量(Symfony 4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆