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

104,571

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.

Share:
104,571
i.am.michiel
Author by

i.am.michiel

Updated on February 27, 2020

Comments

  • i.am.michiel
    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
    Matt Welander over 11 years
    or {{ asset('/uploads/myimage.jpg') }}. Not that the question relates to a view, just saying there is an easier way in twig =)
  • Marius Balčytis
    Marius Balčytis over 11 years
    @MattiasSvensson asset gives me full path, but not uri (not absolute)
  • RayOnAir
    RayOnAir about 11 years
    To me, this solution worked in the development environment as well.
  • flu
    flu over 10 years
    The 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
    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
    Jekis over 10 years
    The question was 'in Controller' not in twig
  • nikans
    nikans over 10 years
    $request = $this->getRequest();
  • RayOnAir
    RayOnAir about 10 years
    This 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
    Todor Todorov almost 10 years
    Not 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
    Todor Todorov almost 10 years
    This will do the trick. After this code concate your folder and the image filename.
  • stryba
    stryba over 9 years
    getRequest() is deprecated in favor of the injection of the Requestobject
  • stryba
    stryba over 9 years
    @barius asset has an absolute option
  • Chicna
    Chicna over 9 years
    Yep, using the extension helper of the "asset" twig function is the most secure way. Thanks !!
  • ke20
    ke20 about 9 years
    works fine with assetic : {% image '@AcmeBundle/Resources/public/images/myimage.jpg' %} <img src="{{ app.request.uriForPath(asset_url) }}"> {% endimage %}
  • fabpico
    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
    Mantas about 8 years
    Keep in mind that templating.helper.assets is only available when php templating engine is enabled.
  • nicolallias
    nicolallias about 8 years
    thanks Nacho, very useful as quick and dirty test ;)
  • COil
    COil over 7 years
    But there is a shortcut as of Symfony 2.1, check my answer.
  • Alister Bulman
    Alister Bulman about 6 years
    with Symfony 3.0+, the equivalent service to fetch asset information within PHP is assets.packages.
  • medskill
    medskill over 5 years
    THX. Working fine in Symfony 3.4.13 . $request->getUriForPath('/uploads/myimage.jpg');