Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access FilterService from another service in Symfony 4 #1135

Closed
chiqui3d opened this issue Nov 3, 2018 · 3 comments
Closed

Access FilterService from another service in Symfony 4 #1135

chiqui3d opened this issue Nov 3, 2018 · 3 comments

Comments

@chiqui3d
Copy link

chiqui3d commented Nov 3, 2018

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?

@lukasluecke
Copy link

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'

@chiqui3d
Copy link
Author

chiqui3d commented Nov 5, 2018

I'm gonna check it out.

@michellesanver
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants