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
app.request.uriForPath('/uploads/myimage.jpg') | replace({'/app_dev.php': ''})
this avoid the problem of app_dev.php
You can use the function getSchemeAndHttpHost() for Gets the scheme and HTTP host and use the function getBasePath() to get the root path from which this request is executed
$request->getSchemeAndHttpHost().$request->getBasePath()."/uploads/myimage.jpg"
This method works fine for the app.php and app_dev.php.
You can get site url i.e point to web folder by using the following code
$protocol = stripos($_SERVER['SERVER_PROTOCOL'],'https') === true ? 'https://' : 'http://';
$hostName = $request->server->get('HTTP_HOST');
$baseDirectory = "";
if(!empty($this->container->get('router')->getContext()->getBaseUrl())){
$baseDirectory = str_replace('app_dev.php', '', $this->container->get('router')->getContext()->getBaseUrl());
}
$rootUrl = $protocol.$hostName.$baseDirectory;