-->

Access Liip Imagine bundle from Controller - assig

2019-08-26 09:35发布

问题:

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.

When I try harmstyler's solution with

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

in my Controller then I get a ServiceNotFoundException

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.

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

In my service.yaml I have

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

In my Controller I have

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

which leads me to the same Exception

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.

[BTW What I am actually trying to do is: I have members and every member has an image. Creating the member I have an image upload and let Liip create resized images of the main image. When I delete the image or the member, of course I also want to delete the cached images by Liip. That's why I try to get the Liip cache manager to be able to get the cached images paths to be able to delete them. Another approach was to have an Event Listener but this didn't work either for me. I will summarize the Listener approach in another question.]

回答1:

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).

You could either:

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