Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to add bootstrap to a symfony app?

I'm using symfony framework 3 to develop a web application. I need to add boostrap's functionality to my application. I installed bootstrap using the below command. (I'm using composer.)

composer require twbs/bootstrap

It installs bootstrap to the application's vendor folder. More specifically vendor\twbs\bootstrap.

I need to attach bootstrap's css and js files in the application's twig templates (located in app\Resources\views) as assets.

e.g.:

<link href="{{ asset('css/bootstrap.css') }}" rel="stylesheet" />

But assets only work with files located in the web (web\bundles\framework) folder. I can copy those .css and .js files from the vendor folder to web folder manually to make this work but there should be a proper way to do it (i.e.: to add assets). e.g.: A command with bin/console?

Any help is appreciated.

like image 477
Wickramaranga Avatar asked Apr 06 '16 13:04

Wickramaranga


People also ask

What is the best way to install Bootstrap?

Steps to Install BootstrapClick on Download; the bootstrap package will be downloaded in a Zip folder. This folder contains the CSS and JS folders. The bootstrap package that is downloaded is ready to use compiled code, which can be easily integrated into the project.

How do I add Bootstrap to an existing project?

Under the folder, copy the extracted files downloaded from bootstrap. Under the head tag of the HTML file, the CSS needs to be linked. The jQuery downloaded should also be copied under the JS file. Make sure that under the project file, the downloaded files and HTML page must be present in that folder.


1 Answers

The suggested approach changed since Symfony version 4: Webpack Encore is used with npm / yarn for bundling the CSS and JavaScript resources, where the Bootstrap framework can be included.

Start by installing Encore and follow with the Bootstrap-specific documentation. In summary, the following commands have to be performed:

composer require symfony/webpack-encore-bundle
yarn install
yarn add bootstrap --dev

# after making the required changes to webpack.config.js, app.js, run Encore
yarn encore dev --watch
like image 106
Siavas Avatar answered Sep 27 '22 22:09

Siavas