Use bootstrap with composer - php

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

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">

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/bootstrap ➔ vendor/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.

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.

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.

Related

Css and Js files in my New Laravel Project are not loading

I was given a Laravel project and I manage to download it's configuration and get it started on php artisan serve.
When I open it on localhost:8000 It opens but only the html portion of it.
It seems like css files didn't load for it.
Is there some sort of package we need to install with composer in my environment.
To get the css working.
The public folder looks like this in the laravel project.
backend favicon.ico front images index.html index.php js robots.txt sql vendor
web.config
Laravel provides a helper function, asset(), which generates a URL for your assets. You can use this in blade syntax.
Put your css, js files on public folder. for example of a css file, put it on :
../public/bootstrap/css/bootstrap.min.css
In your blade you can access this file from header like this :
<link href="{{ asset('bootstrap/css/bootstrap.min.css') }}" rel="stylesheet" type="text/css" >
hope this help

Laravel cannot find stylesheets and nor can I

So I'm very new to Laravel and I'm following a tutorial to use the authentication feature. This works fine, however it is unable to find my scripts and stylesheets. I haven't made any new ones nor have I touched the stylesheet link in app.blade.php, so I have no clue why it is throwing this error.
Code:
<link href="{{ URL::asset('css/app.css') }}" rel="stylesheet">
Error:
GET http://localhost:8000/css/app.css net::ERR_ABORTED 404 (Not Found)
When I go to locahost/css/app.css I get a 404 error. The same error is occurring with app.js. I haven't changed anything about the file structure and it was all generated with composer create-project laravel/laravel
Thanks.
Laravel doesn't include any CSS files by default. You need to create one yourself and put it to /public/ directory, so in your case that would be /public/css/app.css.
Does localhost point to public in your project? Usually you would have to open http://localhost/public
Per default, URL::asset('css/app.css') expects a css file at /public/css/app.css -- for your setup (serving throught php artisan serve, I suppose?), that would be http://localhost:8000/public/css/app.css.
Anyway: If there is no stylesheet, just create one. There is tons of ways how to do it, depending on your needs. Creating one manually for one, but for complex sites think about using laravel mix for compilation. Or anything other way!
Make sure your style exists at public/css directory, then try this code:
<link rel="stylesheet" href="{{asset('css/app.css')}} type="text/css"/>
if the file doesn't exist, create that and try again.
I hope this can help you.

CakePHP 3.4 load jQuery to layout

I started using libraries like jQuery or Twitter Bootstrap with composer. Now I want to load the jquery.min.js from /vendor/components/jquery/ into my /src/Template/Layout/default.ctp.
How can I do that?
Because using a normal uri doesn't work:
<script type="text/javascript" src="/vendor/components/jquery/jquery.min.js"></script>
Cake doesn't interpret the file as a file. It looks for a Controller:
Error: VendorController could not be found.
How can I load the JS-file properly?
This question is perhaps a little broad, but you probably want to look into using something like Gulp or Webpack to build your assets from the Composer files to your app's webroot. These will also enable you to concatenate and minimise all your JS/CSS assets to improve performance. Although personally I'd probably use a CDN to deliver a library like jQuery as it is so commonly used on the web that you can benefit from cross-site caching.
The reason why you are getting an error like "Error: VendorController could not be found." is because Cake can't find the 'vendor' directory inside 'webroot' so is looking for a Controller instead due to the way it handles magic routes (this bit in the routes.php file $routes->fallbacks('DashedRoute');).
If you don't want to use a tool like Gulp or Webpack you could symlink the relevant files in your webroot folder, but personally I'd use my initial suggestions as these will give you many other benefits. If you do symlink just make sure you don't symlink the entire vendor directory as you don't want to expose all the code in there.
You should place all your js in /webroot/js folder. You can use it inline by using:
echo $this->Html->script('jquery.min');
this would load : <script src="/js/jquery.min.js"></script>

Installing Bootstrap theme in Symfony 2 project

I am looking at installing this theme in my SF2 project, and I note that there are 4 files at the top of the page to download, namely:
bootstrap.min.css
bootstrap.css
variables.less
bootswatch.less
I have the less.php (oyejorge/less.php) parser successfully installed along with bootstrap itself, and I have Assetic setup compressing all my CSS together and outputting this to the page:
<link href="/app_dev.php/css/425e28c.css" type="text/css" rel="stylesheet">
However, I would like to install the Superhero theme in the link above but I can't find any tutorials on how to do this? Is it just a case of overwriting variables.less and importing bootswatch.less?
Also read the Usage and Customization sections of the docs at https://github.com/thomaspark/bootswatch.
If you want to use the theme as is (without customization) you only should include the bootstrap.min.css OR bootstrap.css into your HTML:
<link href="/bootstrap.min.css" type="text/css" rel="stylesheet">
Alternatively you can compile your own version of the theme:
Download Bootstrap's source files at http://getbootstrap.com/getting-started/#download
In the source files replace the less/variables.less file with that download from bootswatch
copy the bootswatch.less into the less directory of the bootstrap sources files
Than, open the less/bootstrap.less and add the following line of code at the end of this file: #import "bootswatch"
compile the modified bootstrap as described at: Error while executing assetic dump (parse error: failed at `&:extend(.clearfix all);`)
Also notice that Bootstrap adds the autoprefixer for vendor prefixes into their build process. The vendor prefix mixins in less/mixins/vendor-prefixes.less are deprecated as of v3.2.0. So you should add https://packagist.org/packages/bit3/assetic-autoprefixer to your assetic configuration too.

Symfony Managing Assets directly

Sorry for opening another topic on this but after hours of researching I haven't found a solution:
As stated in symfony's documentation I want to include my assets (css, js, images) directly and without using Assetic. The documentation is recommending the following sniplet:
<script src="{{ asset('js/script.js') }}" type="text/javascript" />
Unfortunately this always ends in a 404 file not found error for every try. I have placed my assets in the myBundle/Resources/public folder and I also copyied it into the web folder using php app/console assets:install web without success.
What am I missing here? Is there a configuration missing? Thank you in advance.
asset('js/script.js') will produce a link like /js/script.js. The files will be copied to a bundles folder, not to the web root. You want something like this to generate the correct URL:
<script src="{{ asset('bundles/myBundle/js/script.js') }}" type="text/javascript"></script>
(Note also that script tags need to have a closing </script> to make them work cross-browser.)

Categories