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

前端 未结 15 2149
悲&欢浪女
悲&欢浪女 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 16:46

    You can generate the url from the request object:

    $baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
    

    You could make a twig extension that cuts the /web part of your path and uses the request to generate the base url.

    see Symfony\Component\Routing\Generator\UrlGenerator::doGenerate for a more solid implementation.

    Also, Twig has access to the request from app.request.

提交回复
热议问题