Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include External CSS and JS file in Laravel 5

Tags:

php

laravel-5

I am Using Laravel 5.0 , the Form and Html Helper are removed from this version , i dont know how to include external css and js files in my header file. Currently i am using this code.

<link rel="stylesheet" href="/css/bootstrap.min.css"> <!---  css file  ---> <script type="text/javascript" src="/js/jquery.min.js"></script> 
like image 714
Mansoor Akhtar Avatar asked Jan 23 '15 06:01

Mansoor Akhtar


People also ask

How do I include external JavaScript and CSS file in HTML?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.


2 Answers

I think that the right way is this one:

<script type="text/javascript" src="{{ URL::asset('js/jquery.js') }}"></script> 

Here I have a js directory in the laravel's app/public folder. There I have a jquery.js file. The function URL::asset() produces the necessary url for you. Same for the css:

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

Hope this helps.

Keep in mind that the old mehods:

{{ Form::script() }} 

and

{{ Form::style() }} 

are deprecated and will not work in Laravel 5!

like image 104
Todor Todorov Avatar answered Sep 29 '22 18:09

Todor Todorov


Try it.

You can just pass the path to the style sheet .

{!! HTML::style('css/style.css') !!} 

You can just pass the path to the javascript.

{!! HTML::script('js/script.js') !!} 

Add the following lines in the require section of composer.json file and run composer update "illuminate/html": "5.*".

Register the service provider in config/app.php by adding the following value into the providers array:

'Illuminate\Html\HtmlServiceProvider'

Register facades by adding these two lines in the aliases array:

'Form'=> 'Illuminate\Html\FormFacade', 'HTML'=> 'Illuminate\Html\HtmlFacade'

like image 34
Jay Dhameliya Avatar answered Sep 29 '22 20:09

Jay Dhameliya