Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
823 views
in Technique[技术] by (71.8m points)

twig - How to get assets img url in Symfony controller

I'm using assets in Symfony 2.7.10 and I want to generate an image url in the controller.

In the symfony config.yml file I have added the following setting:

framework:
    assets:
          version: '311nk2'
          version_format: '%%s?v=%%s'
          base_path: /bundles/myBundleName

In twig the generating of an url works ok, so with the following twig code:

{{ asset('images/logo.png') }}

It generates:

/bundles/myBundleName/images/logo.png?v=311nk2

Now I want to generate the same url in a controller, how is this possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

All right, seeing that this answer get a little bit of attention over the time, I should point out two things:

  • This answer was added when we still had version based tagging in SO and the question was originally added for Sf2
  • Since then, things changed quite a bit, so I'm gonna extend the answer

How to handle asset url for Symfony 2.8.36 using the Container service:

public function indexAction(Request $request)
{
    /** @var SymfonyBundleFrameworkBundleTemplatingHelperAssetsHelper $manager */
    $manager = $this->get('templating.helper.assets');

    return $this->render('default/index.html.twig', array(
        'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
        'logo' => $manager->getUrl('bundles/myBundleName/images/logo.png'),
    ));
}

How to handle asset url for Symfony 3.4.6 using Container service and the new Autowire configuration:

public function indexAction(Request $request)
{
    /** @var SymfonyComponentAssetPackages $manager */
    $manager = $this->get('assets.packages');

    return $this->render('default/index.html.twig', [
        'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
        'logo' => $manager->getUrl('bundles/myBundleName/images/logo.png'),
    ]);
}

and

public function autowireAction(Request $request, SymfonyComponentAssetPackages $assetsManager)
{
    return $this->render('default/index.html.twig', [
        'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
        'logo' => $assetsManager->getUrl('bundles/myBundleName/images/logo.png'),
    ]);
}

How to handle asset url for Symfony 4.0 only with Autowire configuration:

public function indexAction(Request $request, SymfonyComponentAssetPackages $assetsManager)
{
    $path = $assetsManager->getUrl('bundles/myBundleName/images/logo.png');
}

For Symfony 4 there are few things to know:

  • The skeleton of the project has changed a bit, and now you are have to include the packages that you are going to need. In this case, by default Assets management is not included, so in order for this service to work you need to execute composer require symfony/asset.

Original answer as of 22.03.2016

Using the helper service called templating.helper.assets could do the trick. Try something like this:

var_dump($this->get('templating.helper.assets')->getUrl('bundles/myBundleName/images/logo.png'));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...