Search code examples
phplaravellaravel-3

Laravel3: Giving failsafe image paths with built-in functions of Laravel


background-image: url('home_assets/images/origami/parallax/01.png');
<img alt="" src="home_assets/images/origami/logo.png">

I worry about 404 issues if:

  1. I move my Laravel folder into a subfolder,
  2. Rename home_assets in the future

I'm looking for a solution like:

Url::to_home_assets_image('origami/logo.png');

Url will take care of paths. If I move Laravel into a/b/c/ folder, then it should understand public folder should be in a/b/c/public/.

::to_home_assets_image should work like a constant. home_assets_image constant/function/object (whatever you want to call it as) should refer to 'home_assets/image/'.

Something like this: Url::to_home_assets_image('origami/logo.png'); should work.

What do you think? I worry too much or it is something that has to be done to avoid folder issues?

Ps: I can't use HTML::image(). It will break the first example cause it will add an tag.


Solution

  • You can achieve what you're after with <img src="{{URL::to_asset('images/original/logo.jpg')}}">. It would be more work to get those URLs in your CSS styles, but is technically posible. However, assuming all your assets are stored in /public, then url('../images/image.ext') will work fine. If you need to change your asset folder path, you can set an asset_url config variable in your /application/config/application.php.