symfony 4 twig and images

1

I want to upload a simple image with twig.

Reading the symfony manual says that we put this in twig ...

<img src="{{ asset('images/logo.png') }}" alt="Symfony!" />

Well, it does not work.

I have installed assets, $ composer require asset

the logo.png file can be found in the assets / images /

folder

I have also tried to create an addentry in the webpack.config.js file

like this:

.addEntry('logoBoard', './assets/images/logo.png')

but the file does not load.

The file that creates me in public / build / images is logo.ac628998.png, I think this is for the addEntry.

I managed to load it once like this:

<img src="{{ asset('build/images/logo.ac628998.png') }}" alt="Symfony!" />

But I guess this is not the correct way ...

Greetings and thanks.

    
asked by Fernando Diezma 15.05.2018 в 22:36
source

1 answer

1

Try this:

<img src="{{ asset('logo.png', 'imagenlogo') }}" alt="Symfony!" />

The images must be uploaded to a directory within the public folder. For example, if you use the 'images' directory for the images, create the following directory:

public/images

In config / packages / framework.yaml you must add:

framework:
    assets:
        packages:
            imagenlogo:
                base_path: 'images'

Do not forget to move the logo.png image to public / images and empty the cache before reloading the application.

    
answered by 16.05.2018 / 16:35
source