Symfony Webpack/Encore processing unreferenced source images
Create a
s
subdirectory under web root:Symfony 3:
web/s
Symfony 4 & 5:
public/s
Configure your new subdomain:
Symfony 3: (
app/config/config.yml
)framework: assets: base_urls: - '%protocol%://s.%host%'
Symfony 4 & 5: (
config/packages/assets.yaml
)framework: assets: base_urls: - '%protocol%://s.%host%'
Note:
%protocol%
is eitherhttp
orhttps
accoring to you environment%host%
is your host, maybeexample.com
or something.
Configure Webpack (
webpack.config.js
)Symfony 3:
var Encore = require('@symfony/webpack-encore');Encore // ... .setOutputPath('./web/s/build/') .setPublicPath('/build') .setManifestKeyPrefix('build') .cleanupOutputBeforeBuild() // ...;module.exports = Encore.getWebpackConfig();
Symfony 4 & 5:
var Encore = require('@symfony/webpack-encore');Encore // ... .setOutputPath('./public/s/build/') .setPublicPath('/build') .setManifestKeyPrefix('build') .cleanupOutputBeforeBuild() // ...;module.exports = Encore.getWebpackConfig();
Put all your images inside
/web/s
or/public/s
according to your Symfony version.Inside your Twig views, access them like below:
<img src="{{ asset('photo.jpg') }}" alt="Photo"/>
Accept and hit upvote on this cool answer!
In webpack.config.js
, you could add .copyFiles()
to the Encore object. This will take an object with "from", and "to" keys, with a couple of wildcards.
Here is an example:
.copyFiles({ from: 'src', to: 'images/[path][name].[ext]'})