如何在SF4的Twig扩展中注入依赖项

时间:2018-10-22 21:12:23

标签: symfony dependency-injection symfony4

我试图将Assets依赖项添加到TwigExtension中,但是最后得到了:

  

无法自动装配服务“ App \ Twig \ AppExtension”:方法“ __construct()”的参数“ $ urlPackage”引用类“ Symfony \ Component \ Asset \ UrlPackage”,但不存在此类服务。

所以这基本上就是我正在做的:

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");

当然,我已经检查了天气或该软件包是否存在:

/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  

到底怎么了?我也可以在services.yml中做点什么吗?

1 个答案:

答案 0 :(得分:2)

  

我也可以在services.yml中做什么吗?

是的,您需要定义一个新的服务别名,以根据其FQCN完成自动装配:

services:
    # ...

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