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
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.