Baldráni
Baldráni

Reputation: 5640

How to Inject Dependencies in Twig Extension in SF4

I'm trying to add the Assets dependencies to a TwigExtension however I'm ending up with :

Cannot autowire service "App\Twig\AppExtension": argument "$urlPackage" of method "__construct()" references class "Symfony\Component\Asset\UrlPackage" but no such service exists.

So this is basically what I'm doing :

use Symfony\Component\Asset\UrlPackage;

...

protected $urlPackage;
public function __construct(UrlPackage $urlPackage)
{
    $this->urlPackage = $urlPackage;
}

....

$url = $this->urlPackage->getUrl("build/assets/images/svg/notification.svg");

And of course I've checked weather or not the package is existing :

/var/www/fromton # bin/console debug:container | grep assets
  Symfony\Component\Asset\Packages                                                     alias for "assets.packages"                                                                     
  assets._default_package                                                              Symfony\Component\Asset\PathPackage                                                             
  assets._version__default                                                             Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy                             
  assets.context                                                                       Symfony\Component\Asset\Context\RequestStackContext                                             
  assets.empty_package                                                                 Symfony\Component\Asset\Package                                                                 
  assets.empty_version_strategy                                                        Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy                                    
  assets.json_manifest_version_strategy                                                Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy                             
  assets.packages                                                                      Symfony\Component\Asset\Packages                                                                
  assets.path_package                                                                  Symfony\Component\Asset\PathPackage                                                             
  assets.static_version_strategy                                                       Symfony\Component\Asset\VersionStrategy\StaticVersionStrategy                                   
  assets.url_package                                                                   Symfony\Component\Asset\UrlPackage                                                              
  console.command.assets_install                                                       Symfony\Bundle\FrameworkBundle\Command\AssetsInstallCommand                                     
  twig.extension.assets                                                                Symfony\Bridge\Twig\Extension\AssetExtension  

What the heck is wrong? Shall I do something in services.yml too?

Upvotes: 4

Views: 1268

Answers (1)

yceruto
yceruto

Reputation: 9585

Shall I do something in services.yml too?

Yes, you need to define a new service alias completing the autowiring based on its FQCN:

services:
    # ...

    Symfony\Component\Asset\UrlPackage: '@assets.url_package'

Upvotes: 4

Related Questions