How to get the full url for an asset in Controller?

前端 未结 15 2134
悲&欢浪女
悲&欢浪女 2020-12-12 16:13

I need to generate some JSON content in controller and I need to get the full URL to an uploaded image situated here : /web/uploads/myimage.jpg.

How can

15条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 17:03

    The best solution would be IMHO to look at Twig's implementation of asset, which you can find in:

    \Symfony\Bundle\TwigBundle\Extension\AssetsExtension
    

    in the method:

    public function getAssetUrl($path, $packageName = null, $absolute = false, $version = null)
    

    which basically uses the templating.helper.assets service similar to @iamdto's answer:

    $url = $this->container->get('templating.helper.assets')->getUrl($path, $packageName, $version);
    

    Or directly use the AssetsExtension service or class respectively.

提交回复
热议问题