How to include External CSS and JS file in Laravel 5

后端 未结 14 1764
庸人自扰
庸人自扰 2020-11-30 19:18

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 usin

相关标签:
14条回答
  • 2020-11-30 19:35

    There are many ways to include external css and js files as specified in below code but you can choose one of them, i have tested them all, they work the same.

    <link rel="stylesheet" href="{{ asset('css/stylesheet.css') }}" /><!--Recommended-->
    <link rel="stylesheet" href="{{ URL::asset('css/somestylesheet.css') }}" />
    <link rel="stylesheet" href="{{ URL::to('css/otherstylesheet.css') }}" />
    <link rel="stylesheet" href="{{ url('css/anotherstylesheet.css') }}" />
    

    Maybe there are more ways to load it.

    but keep in mind that if you are loading the css and js from other than a public folder then better to use Laravel mix.

    0 讨论(0)
  • 2020-11-30 19:43

    In laravel 5.1,

     <link rel="stylesheet" href="{{URL::asset('assets/css/bootstrap.min.css')}}">
     <script type="text/javascript" src="{{URL::asset('assets/js/jquery.min.js')}}"></script>
    

    Where assets folder location is inside public folder

    0 讨论(0)
  • 2020-11-30 19:44

    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!

    0 讨论(0)
  • 2020-11-30 19:44

    Place your assets in public directory and use the following

    URL::to()
    

    example

    <link rel="stylesheet" type="text/css" href="{{ URL::to('css/style.css') }}">
    <script type="text/javascript" src="{{ URL::to('js/jquery.min.js') }}"></script>
    
    0 讨论(0)
  • 2020-11-30 19:45

    I may be late to the party but it's easy to include JS and CSS the Laravel Way by simply using asset() function

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

    Hope that helps

    0 讨论(0)
  • 2020-11-30 19:47

    I have been making use of

    <script type="text/javascript" src="{{ URL::asset('js/jquery.js') }}"></script> 
    for javascript and 
         <link rel="stylesheet" href="{{ URL::asset('css/main.css') }}">
    for css, this points to the public directory, so you need to keep your css and js files there.
    
    0 讨论(0)
提交回复
热议问题