Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use an image from public folder

I'm trying to set as background an image located at /public/images/my-image.jpg

I've tried

<body background="public/images/my-image.jpg">

and

<body background="images/my-image.jpg">

and

<body background="my-image.jpg">

But I always get 404 (Not Found) on the Chrome console. Any idea why?

I also tried adding this:

<style>
    body {
        background-image: url("/public/images/my-image.jpg");
    }
</style>

But nothing appears in the background of the page.

like image 318
octavian Avatar asked Oct 29 '25 08:10

octavian


2 Answers

You need to have a record in the routes file

GET     /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)

Then you can access the files in the public folder with the twirl helper:

<body background="@routes.Assets.versioned("images/my-image.jpg")">

It would be compiled to

<body background="/assets/images/my-image.jpg")">

You can put it as static text as well.

If you want to change "assets" to "public" or whatever, just change it in the routes file:

GET     /public/*file               controllers.Assets.versioned(path="/public", file: Asset)

Then your assets would be accessible by the public path, like:

<body background="/public/images/my-image.jpg")">

Still, the @routes.Assets.versioned would be the same:

<body background="@routes.Assets.versioned("images/my-image.jpg")">

This is the reason why @routes.Assets.versioned is preferable way.

like image 83
Andriy Kuba Avatar answered Oct 30 '25 22:10

Andriy Kuba


As I understand it, you've got a problems obtaining assets in playframework. Follow the documentation for play framework for assets.

To obtain assets from public directory (if you haven't change the default routes or assets controller), you need to use path with assets/ instead of public/. Or more preferable using reverse router. In play 2.5x in your case it would be:

<body background="@routes.Assets.versioned("images/my-image.jpg")">

using reverse routing or

<body background="assets/images/my-image.jpg">

with hard coded path.

like image 36
Rumid Avatar answered Oct 30 '25 23:10

Rumid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!