background-image and the relative routes

0

You see, I want to use an image with this code for the user bar:

<nav class="navbar navbar-expand-md navbar-laravel"
style="background-image: url('usuario/sacarino.jpg');">

With this I get to see the image:

But in case it is a more complex address, like those in which it is filtered according to a variable, it can fail and the image can not be shown.

I have the image in the public / user folder. How do I access it no matter where it is?

    
asked by Miguel Alparez 16.11.2018 в 09:43
source

1 answer

1

You could use an absolute path to reference the image, type:

https://miSitioWeb/public/usuario/nombreDeTuImagen.png

But since you are using a framework , you could take advantage of the fact that these usually have functions that return the url base of the site. In Laravel you have different ways depending on where you want to get it, when trying to do it from a view I recommend:

For versions below 5.2

echo url();

For versions 5.2 or higher

echo url('/');

In your HTML you just have to add the following to the style attribute:

<nav class="navbar navbar-expand-md navbar-laravel"
style="background-image: url("<?php echo url('/public/usuario/nombreDeTuImagen.png') ?>");">

Reference Laravel

    
answered by 16.11.2018 / 10:23
source