How to get the full url for an asset in Controller?
Solution 1
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
.
Solution 2
You can use this in Controller:
$this->getRequest()->getUriForPath('/uploads/myimage.jpg');
EDIT : This method also includes the app.php
and app_dev.php
to the url. Meaning this will only work in production when url-rewriting is enabled!
Solution 3
You can use the templating.helper.assets
service.
First define your assets base URL :
# app/config/config.yml
framework:
templating:
assets_base_url: "http://www.mywebsite.com/"
Then just call the service from within your controller, command or wherever you are :
<?php
// src/Acme/Bundle/DemoBundle/Controller/DemoController.php
namespace Acme\Bundle\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DemoController extends Controller
{
public function indexAction()
{
$myAssetUrl = $this
->get('templating.helper.assets')
->getUrl('bundles/acmedemo/js/main.js', $packageName = null)
;
// $myAssetUrl is "http://www.mywebsite.com/bundles/acmedemo/js/main.js"
return array();
}
}
Solution 4
If you want to get this url from within a twig template use:
{{ app.request.uriForPath('/uploads/myimage.jpg') }}
Solution 5
As of Symfony 2.1 you can use:
$request->getSchemeAndHttpHost().'/uploads/myimage.jpg';
Note: This solution has the advantage to work in both DEV and PROD environments.
i.am.michiel
Updated on February 27, 2020Comments
-
i.am.michiel about 4 years
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 I get the full url of it?
http://www.mywebsite.com/uploads/myimage.jpg
-
Matt Welander over 11 yearsor {{ asset('/uploads/myimage.jpg') }}. Not that the question relates to a view, just saying there is an easier way in twig =)
-
Marius Balčytis over 11 years@MattiasSvensson
asset
gives me full path, but not uri (not absolute) -
RayOnAir about 11 yearsTo me, this solution worked in the development environment as well.
-
flu over 10 yearsThe questioner asked for controller code, this is twig. If you don't want app_dev.php added to your URLs you should simply use the prod controller (app.php or simply "/" if it's configured right).
-
Prisoner over 10 years@RayOnAir, the problem is that if you're using the
app_dev.php
url, an asset will be appended to this url, e.g.http://bla.com/app_dev.php/uploads/test.png
- which doesn't exist. -
Jekis over 10 yearsThe question was 'in Controller' not in twig
-
nikans over 10 years
$request = $this->getRequest();
-
RayOnAir about 10 yearsThis is a great solution, as it is the most flexible one! You can put your assets anywhere you want (i.e. a CDN) in the future and only change the assets_base_url value in your config file.
-
Todor Todorov almost 10 yearsNot working in Symfony 2.5. This code returns URL with the app.php or app_dev.php included in the path. By the way it can be used, but you got to use str_replace() to produce legal URL.
-
Todor Todorov almost 10 yearsThis will do the trick. After this code concate your folder and the image filename.
-
stryba over 9 years
getRequest()
is deprecated in favor of the injection of theRequest
object -
stryba over 9 years@barius asset has an absolute option
-
Chicna over 9 yearsYep, using the extension helper of the "asset" twig function is the most secure way. Thanks !!
-
ke20 about 9 yearsworks fine with assetic : {% image '@AcmeBundle/Resources/public/images/myimage.jpg' %} <img src="{{ app.request.uriForPath(asset_url) }}"> {% endimage %}
-
fabpico over 8 years
$request = $this->container->get('request');
when you can't inject the Request object (e.g. overwriting setContainer() that has a predefined signature) -
Mantas about 8 yearsKeep in mind that
templating.helper.assets
is only available whenphp
templating engine is enabled. -
nicolallias about 8 yearsthanks Nacho, very useful as quick and dirty test ;)
-
COil over 7 yearsBut there is a shortcut as of Symfony 2.1, check my answer.
-
Alister Bulman about 6 yearswith Symfony 3.0+, the equivalent service to fetch asset information within PHP is
assets.packages
. -
medskill over 5 yearsTHX. Working fine in Symfony 3.4.13 . $request->getUriForPath('/uploads/myimage.jpg');