37

I'm a web newbie programmer,
I'm trying to learn from home to make my own web. I have notions of php, html, js and css.
But I've found something that is not how to solve. I'm trying to use Composer to manage Bootstrap. I installed Composer and I have run this line of code

composer require twbs/bootstrap

that has dropped a folder with files.

I do not understand is how I make html links to find the js and css files, you should do indicating the full path?

vendor / twbs / bootstrap / dist / js / bootstrap.js

Excuse me if the question is stupid but I do not know how I should continue.
Amd excuse my English, I'm learning too but by now I use google translate

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
James Ryan
  • 371
  • 1
  • 3
  • 4

4 Answers4

35

You could use a post update command in the composer.json file:

"scripts": {
        "post-update-cmd": [
            "rm -rf public/bootstrap",
            "cp -R vendor/twbs/bootstrap/dist public/bootstrap"
        ]
    }

And then just include the javascript- and css-files like this:

<script type="text/javascript" src="{{ ROOT_URL }}bootstrap/js/bootstrap.min.js"></script>
<link href="{{ ROOT_URL }}bootstrap/css/bootstrap.min.css" rel="stylesheet">
WebWorker
  • 413
  • 3
  • 9
Rahul Shinde
  • 853
  • 9
  • 7
  • Ok just to be sure, you need to make those public/bootstrap folders first or try the bootstrap script first without "rm" command. Then, when including scripts, you can add scripts only adding href="/public/bootstrap/css/bootsrtap.min.css" and the same for src and js script. The best answer I can find! – Boky Feb 26 '23 at 15:31
  • src="{{ ROOT_URL }}bootstrap/js/bootstrap.min.js" You can replace your {{ ROOT_URL }} with a slash, when using Domains – Wolfgang Blessen Aug 31 '23 at 09:28
22

Yes, Composer downloads the dependencies by default into the vendor folder. So Bootstrap will also land in the vendor folder, which is not the correct place to reference it or include it.

composer require twbs/bootstrapvendor/twbs/bootstrap/dist/js/bootstrap.js


Your next step would be to write a little helper script to copy the Boostrap files you need, into your public/assets folder. You could copy the complete dist folder including sub-folders (vendor\twbs\bootstrap\dist) into public or public\assets.

Please overwrite existing files, e.g. if a file exists remove it, then copy it. This allows to easily update the files, when you need to update the Bootstrap vendor package again.

Of course, you could also just copy the files manually or create a symlink. It depends.

That gives you the following directory structure:

public
 \- assets
    |- css
    |- js
    \- fonts
 \- index.html

When the Boostrap assets are copied you can start to include them, in your index.html or template (assets\js\bootstrap.min.js, etc.).


Referencing: https://stackoverflow.com/a/34423601/1163786 which shows also other solutions to this problem, e.g. fxp/composer-asset-plugin, bower, grunt.

Community
  • 1
  • 1
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
  • What is the idea of the composer which is a PHP package manager to download Bootstrap (front-end CSS lib) into a place where it then needs to be copied to another place using a script? – Adelin Jul 02 '21 at 15:32
  • Bootstrap is a front-end dependency and it should be managed by a "front-end package manager". If Composer is used, then a front-end package lands in a backend folder (vendor), which might not be a web-served folder (not public_html). So copying might be required! – Jens A. Koch Jul 02 '21 at 17:55
  • it is not necessary to copy them to another path, only the files should be included in the headers – Bayron Vazquez Nov 14 '21 at 01:38
8

Unless you need customization inside bootstrap (e.g. building scss), the most simple solution is relying on a CDN (as a bonus, you get super-fast caching of assets)

So, simply call your assets like so:

<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<script src="//code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

Composer is a super-excellent tool for backend dependencies, but not the best one for frontend.

Massimiliano Arione
  • 2,422
  • 19
  • 40
-6

If you want to have the files in your server, and you don't want to use npm, grunt or another frontend library manager, you can simply download the files listed in Massimiliano's answer and put them inside your js/css folders:

First download these files (updated to the most recent Bootstrap 3 version):

http://netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css
http://code.jquery.com/jquery-2.2.4.min.js
http://netdna.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js

And put them in these folders (starting from the root of your project):

public/css/bootstrap.min.css
public/js/jquery-2.2.4.min.js
public/js/bootstrap.min.js

Now, you want to be able to call them in the blade templates for your views. It's simple: just add <link> and <script> tags as normal for any css/js file, adding the following to your blade template:

<link href="{{ url('css/bootstrap.min.css') }}" rel="stylesheet" type="text/css" />
<script src="{{ url('js/jquery-2.2.4.min.js')}}"></script>
<script src="{{ url('js/bootstrap.min.js')}}"></script>

P.s.: if you see the console, it shows something as the following error message:

Source map error: request failed with status 404
Resource URL: http://localhost:8000/css/bootstrap.min.css
Source Map URL: bootstrap.min.css.map

This will not affect the style of your page, and you can simply ignore this message, or remove a line from the bootstrap file as the answers to this question suggest. This answer explain a bit more.

Brian Hellekin
  • 538
  • 7
  • 23
  • Although the askers directly asks how to use bootstrap with composer, you basically transformed the question to a very beginners one, how to include css into a HTML page, one of most basic web development question ever existing, except of public document root path which was already answered two times before you but they also mentioned how to include it. You also reference 3 links via which is not figurable if they are local, one is local question, two other questions and quite unrelated. I realize the effort to help, though I don't think other questions belongs to this one. – FantomX1 May 15 '20 at 13:56