Asset Mapper

Contents
Import maps
Link assets
Compile
Versioned URLs

Import maps

Asset Mapper serves your CSS and JS without a build step. The file importmap.php maps short names to files inside assets, and the browser loads them as modules. One entry per file, one flag for the entrypoint.

// importmap.php: app maps to ./assets/app.js as entrypoint

return app => path ./assets/app.js, entrypoint true

Link assets

Load the entrypoint in your layout with one Twig line. The helper prints the script tags for the entrypoint and everything it imports, so templates never hardcode file paths.

Twig layout block: importmap helper with name app

Renders script tags for app and its imports

Compile

For production, compile the assets once during deploy. The command copies every mapped file into public/assets with a versioned name, so the live server serves plain static files.

$ php bin/console asset-map:compile

Compiled assets written to public/assets/

Versioned URLs

Compiled files carry a content hash, so browsers cache them for a long time and still fetch the new version after each deploy. The asset() helper prints the hashed URL for you, with no manual renaming.

Template call: asset helper with images/logo.png

/assets/images/logo-a1b2c3.png

Next: Deployment

Article author: Arthur Isaev

Related articles
Development with PHP
What is Symfony and where it runs
Install Symfony
Your first page
Routes
Controllers
Twig basics
Twig advanced
Doctrine setup
Entities
Migrations
Repositories
Relations
Forms
Validation
Services and DI
JSON controllers
Serializer
Security intro
Users
Login form
Voters
Console commands
Events
Messenger
Mailer
Cache
Sessions
CSRF
Translations
Dotenv and env
Profiler
Testing
Fixtures
Pagination
File uploads
API Platform in one lesson
DQL
Transactions
Migrations advanced
Security advanced
HTTP client
Scheduler
Webhooks
Asset Mapper
Deployment
Performance
Capstone: blog
Capstone: JSON API
Modern Symfony tour
Hero roadmap: the whole course on one page

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: