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 SensioLabs
  1. Home
  2. Documentation
  3. Cookbook
  4. Frontend
  5. Using Bower with Symfony
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Installing Bower
  • Configuring Bower in your Project
  • An Example: Installing Bootstrap
    • Installing the Dependency
    • Including the Dependency in your Template
    • Should I Git Ignore or Commit Bower Assets?

Using Bower with Symfony

Edit this page

Warning: You are browsing the documentation for Symfony 2.3, which is no longer maintained.

Read the updated version of this page for Symfony 6.3 (the current stable version).

Using Bower with Symfony

Symfony and all its packages are perfectly managed by Composer. Bower is a dependency management tool for front-end dependencies, like Bootstrap or jQuery. As Symfony is purely a back-end framework, it can't help you much with Bower. Fortunately, it is very easy to use!

Installing Bower

Bower is built on top of Node.js. Make sure you have that installed and then run:

1
$ npm install -g bower

After this command has finished, run bower in your terminal to find out if it's installed correctly.

Tip

If you don't want to have NodeJS on your computer, you can also use BowerPHP (an unofficial PHP port of Bower). Beware that this is currently in beta status. If you're using BowerPHP, use bowerphp instead of bower in the examples.

Configuring Bower in your Project

Normally, Bower downloads everything into a bower_components/ directory. In Symfony, only files in the web/ directory are publicly accessible, so you need to configure Bower to download things there instead. To do that, just create a .bowerrc file with a new destination (like web/assets/vendor):

1
2
3
{
    "directory": "web/assets/vendor/"
}

Tip

If you're using a front-end build system like Gulp or Grunt, then you can set the directory to whatever you want. Typically, you'll use these tools to ultimately move all assets into the web/ directory.

An Example: Installing Bootstrap

Believe it or not, but you're now ready to use Bower in your Symfony application. As an example, you'll now install Bootstrap in your project and include it in your layout.

Installing the Dependency

To create a bower.json file, just run bower init. Now you're ready to start adding things to your project. For example, to add Bootstrap to your bower.json and download it, just run:

1
$ bower install --save bootstrap

This will install Bootstrap and its dependencies in web/assets/vendor/ (or whatever directory you configured in .bowerrc).

See also

For more details on how to use Bower, check out Bower documentation.

Including the Dependency in your Template

Now that the dependencies are installed, you can include bootstrap in your template like normal CSS/JS:

1
2
3
4
5
6
7
8
9
10
11
12
{# app/Resources/views/layout.html.twig #}
<!doctype html>
<html>
    <head>
        {# ... #}

        <link rel="stylesheet"
            href="{{ asset('assets/vendor/bootstrap/dist/css/bootstrap.min.css') }}">
    </head>

    {# ... #}
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- app/Resources/views/layout.html.php -->
<!doctype html>
<html>
    <head>
        {# ... #}

        <link rel="stylesheet" href="<?php echo $view['assets']->getUrl(
            'assets/vendor/bootstrap/dist/css/bootstrap.min.css'
        ) ?>">
    </head>

    {# ... #}
</html>

Great job! Your site is now using Bootstrap. You can now easily upgrade bootstrap to the latest version and manage other front-end dependencies too.

Should I Git Ignore or Commit Bower Assets?

Currently, you should probably commit the assets downloaded by Bower instead of adding the directory (e.g. web/assets/vendor) to your .gitignore file:

1
$ git add web/assets/vendor

Why? Unlike Composer, Bower currently does not have a "lock" feature, which means that there's no guarantee that running bower install on a different server will give you the exact assets that you have on other machines. For more details, read the article Checking in front-end dependencies.

But, it's very possible that Bower will add a lock feature in the future (e.g. bower/bower#1748).

If you don't care too much about having exact the same versions, you can only commit the bower.json file. Running bower install will give you the latest versions within the specified version range of each package in bower.json. Using strict version constraints (e.g. 1.10.*) is often enough to ensure only bringing in compatible versions.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:
    Symfony Code Performance Profiling

    Symfony Code Performance Profiling

    Code consumes server resources. Blackfire tells you how

    Code consumes server resources. Blackfire tells you how

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of DerManoMann, a Symfony contributor

    Thanks DerManoMann for being a Symfony contributor

    4 commits • 286 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?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • 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

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

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

      Deployed on

    Follow Symfony

    Search by Meilisearch