Liipimaginebundle: Access FilterService from another service in Symfony 4

Created on 3 Nov 2018  路  3Comments  路  Source: liip/LiipImagineBundle

I'm trying to access filter service, but I can't do it, and I can't find anything related to the services in the documentation.

<?php
namespace App\EventSubscriber;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use App\Service\Configuration;
use Vich\UploaderBundle\Event\Event;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Liip\ImagineBundle\Service\FilterService;
use Liip\ImagineBundle\Factory\Config\FilterFactoryInterface;
use Liip\ImagineBundle\Controller\ImagineController;


class VichSubscriber implements EventSubscriberInterface
{

    private $configuration;
    private $container;
    private $filterService;

    public function __construct(FilterService $filterService,Configuration $configuration, ContainerInterface $container)
    {
        $this->configuration = $configuration;
        $this->container = $container;
        $this->filterService = $filterService;
    }

    public static function getSubscribedEvents()
    {
        return array(
           'vich_uploader.post_upload' => 'onVichUploaderPostUpload',
        );
    }

    public function onVichUploaderPostUpload(Event $event)
    {
        $imageName = $event->getObject()->getImageName();
        $path      = $event->getMapping()->getUploadDestination();
        $resourcePath = $this->filterService->getUrlOfFilteredImage($path.$imageName, 'my_thumb');

        dd($resourcePath);
    }
}

Error:

Cannot autowire service "App\EventSubscriber\VichSubscriber": argument "$filterService" of method "__construct()" references class "Liip\ImagineBundle\Service\FilterService" but no such service exists. You should maybe alias this class to the existing "liip_imagine.service.filter" service.

Then trying what it says in the configuration.

$imagine = $this ->container->get('liip_imagine.service.filter');

Error:

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.

The message says to use dependency injection, but it's what I used before and it doesn't work either, so I'm missing out?

Most helpful comment

You can add the following to your services configuration (e.g. services.yaml) in order to use dependency injection.

Liip\ImagineBundle\Service\FilterService:
        alias: 'liip_imagine.service.filter'

All 3 comments

You can add the following to your services configuration (e.g. services.yaml) in order to use dependency injection.

Liip\ImagineBundle\Service\FilterService:
        alias: 'liip_imagine.service.filter'

I'm gonna check it out.

Seems like this was solved! Please open a new issue if this is still a problem for you @chiqui3d :)

Was this page helpful?
0 / 5 - 0 ratings