Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Front-end Tools: Handling CSS & JavaScript

Front-end Tools: Handling CSS & JavaScript

Edit this page

Symfony gives you the flexibility to choose any front-end tools you want. There are generally two approaches:

  1. building your HTML with PHP & Twig;
  2. building your frontend with a JavaScript framework like React, Vue, Svelte, etc.

Both work great - and are discussed below.

Using PHP & Twig

Symfony comes with two powerful options to help you build a modern and fast frontend:

  • AssetMapper (recommended for new projects) runs entirely in PHP, doesn't require any build step and leverages modern web standards.
  • Webpack Encore is built with Node.js on top of Webpack.
  AssetMapper Encore
Production Ready? yes yes
Stable? yes yes
Requirements none Node.js
Requires a build step? no yes
Works in all browsers? yes yes
Supports Stimulus/UX yes yes
Supports Sass/Tailwind yes yes
Supports React, Vue, Svelte? yes [1] yes
Supports TypeScript yes yes
Removes comments from JavaScript no yes
Removes comments from CSS no no
Versioned assets always optional
Can update 3rd party packages yes no [2]

[1] Using JSX (React), Vue, etc with AssetMapper is possible, but you'll need to use their native tools for pre-compilation. Also, some features (like Vue single-file components) cannot be compiled down to pure JavaScript that can be executed by a browser.

[2] If you use npm, there are update checkers available (e.g. npm-check).

AssetMapper (Recommended)

AssetMapper is the recommended system for handling your assets. It runs entirely in PHP with no complex build step or dependencies. It does this by leveraging the importmap feature of your browser, which is available in all browsers thanks to a polyfill.

Read the AssetMapper Documentation

Webpack Encore

Screencast

Do you prefer video tutorials? Check out the Webpack Encore screencast series.

Webpack Encore is a simpler way to integrate Webpack into your application. It wraps Webpack, giving you a clean & powerful API for bundling JavaScript modules, pre-processing CSS & JS and compiling and minifying assets.

Read the Encore Documentation

Switch from AssetMapper

By default, new Symfony webapp projects (created with symfony new --webapp myapp) use AssetMapper. If you still need to use Webpack Encore, use the following steps to switch. This is best done on a new project and provides the same features (Turbo/Stimulus) as the default webapp.

1
2
3
4
5
6
7
8
9
# Remove AssetMapper & Turbo/Stimulus temporarily
$ composer remove symfony/ux-turbo symfony/asset-mapper symfony/stimulus-bundle

# Add Webpack Encore & Turbo/Stimulus back
$ composer require symfony/webpack-encore-bundle symfony/ux-turbo symfony/stimulus-bundle

# Install & Build Assets
$ npm install
$ npm run dev

Stimulus & Symfony UX Components

Once you've installed AssetMapper or Webpack Encore, it's time to start building your front-end. You can write your JavaScript however you want, but we recommend using Stimulus, Turbo and a set of tools called Symfony UX.

To learn about Stimulus & the UX Components, see the StimulusBundle Documentation

Using a Front-end Framework (React, Vue, Svelte, etc)

If you want to use a front-end framework (Next.js, React, Vue, Svelte, etc), we recommend using their native tools and using Symfony as a pure API. A wonderful tool to do that is API Platform. Their standard distribution comes with a Symfony-powered API backend, frontend scaffolding in Next.js (other frameworks are also supported) and a React admin interface. It comes fully Dockerized and even contains a web server.

Other Front-End Articles

  • Create a UX bundle
  • How to Use a Custom Version Strategy for Assets
  • Passing Information from Twig to JavaScript
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version

    Symfony 7.2 is backed by

    Code consumes server resources. Blackfire tells you how

    Code consumes server resources. Blackfire tells you how

    Get your Sylius expertise recognized

    Get your Sylius expertise recognized

    Version:

    Table of Contents

    • Using PHP & Twig
      • AssetMapper (Recommended)
      • Webpack Encore
      • Stimulus & Symfony UX Components
    • Using a Front-end Framework (React, Vue, Svelte, etc)
    • Other Front-End Articles

    Symfony footer

    Avatar of HONORE HOUNWANOU, a Symfony contributor

    Thanks HONORE HOUNWANOU (@mercuryseries) for being a Symfony contributor

    4 commits • 8 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony