1

I'm uploading and saving images to the assets folder and I need to get the absolute path. I could have hard coded it but it would be all wrong if I was to deploy the app to a server with different folder structure. So I need the Asset component.

In the twig template, I can call

<img src="{{ asset("assets/img/" ~ image) }}">

and it will all work. But how do I use it in the controller and return only the path?

reddy
  • 1,721
  • 3
  • 16
  • 26
  • possible duplicate: https://stackoverflow.com/a/16498845/3693616 – Romain Norberg Mar 21 '18 at 09:23
  • You could find your answer here : https://stackoverflow.com/questions/36163803/how-to-get-assets-img-url-in-symfony-controller – Bouffe Mar 21 '18 at 13:22
  • I don't want to return the path for the file, I want to get path of the assets folder, something like http://example.com/assets/ – reddy Mar 21 '18 at 16:13

1 Answers1

-1

You can try this:

use Symfony\Component\Asset\Package;
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;

$package = new Package(new EmptyVersionStrategy());

$imageUrl = $package->getUrl('/');
EmilCataranciuc
  • 1,024
  • 1
  • 11
  • 24