Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fsdolphin's avatar

Understanding URLs and mage links

This is a very basic question. Why does the route/URL affects the location of an image?

| ------------------------------------ 1 - Case ONE: -----------------------------------------|

This works fine image can be accessed...

Image Location:

public > images > my-image.png

Route:

Route::get('apps', function () {
    return view('apps');
});

View:

resources > views > apps.blade.php

    <img src="images/my-image.png" alt="">      

| ------------------------------------ 2 - Case TWO: -----------------------------------------|

This does NOT work, image CANNOT be accessed any more...

Image Location:

public > images > my-image.png

Route:

Route::get('apps/appName', function () {
 return view('appName');
});

View:

resources > views > appName.blade.php

    <img src="images/my-image.png" alt="">

In theory the only difference between the two cases is the URL. Can someone explain how does the URL affects the location of the image? I thought that the routs was just that a URL not the folder structure.

My point is that if in both cases the page appName.blade.php is in the same location and pointing to the same folder structure where the image resides why the second case doesn't work. Perhaps I don't understand how URLs work in general, sorry.

0 likes
6 replies
bobbybouwmann's avatar
Level 88

So resources URL's only work from the route. So my prefixing your image with a / you will make sure it always starts looking at the root of your site. In this case of Laravel it's the public directory.

<img src="/images/my-image.png" alt="">
1 like
Repox's avatar

Or:

<img src="{{ url('images/my-image.png') }}" alt="">
1 like
bobbybouwmann's avatar

@Repox Good example, only my point was that you need to start at the root of the public directory with resource paths. Although your answer is correct, it doesn't explain why it's correct.

The url method will create a url based on your config/current domain and based on the given path. So the example would return

http://example.com/images/my-image.png

1 like
fsdolphin's avatar

@bobbybouwmann / @Repox Both examples worked, now the question is, is there any benefit when using one over the other?

<img src="/images/my-image.png" alt="">

VS

<img src="{{ url('images/my-image.png') }}" alt="">
Repox's avatar

@bobbybouwmann

Although your answer is correct, it doesn't explain why it's correct.

Well, you explained the problem, I just gave an alternative solution.

@fsdolphin

Both examples worked, now the question is, is there any benefit when using one over the other?

I guess that depends.

Using the / will always point to the root of the URI, meaning you can't work with the application in subdirectories. Say your domain is example.com and you want to run your app inside a subfolder (like example.com/beta), you have to do /beta/images/my-image.png to make it work. It's going to be a hoot trying to fix that when moving the application up a folder.

Using url() you'll have the benefit of combining Laravels features for generating URL's based on qualified guesses or the configuration you set, which makes it trivial to move the application to whatever directory you want if you need to.

Personally, I prefer url().

1 like

Please or to participate in this conversation.