How to include External CSS and JS file in Laravel 5

后端 未结 14 1616
庸人自扰
庸人自扰 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:21

    CORRECT & SECURED WAY


    If you have a external .css or .js file to add into your page!

    My version : Laravel Framework 5.7

    If you want to add a External .js file..

    1. Copy your External codes.
    2. Run command npm install in your terminal.
    3. When the above command is executed you can see a folder named node_modules.
    4. Then go to resources/js.
    5. Open file app.js.
    6. Scroll down to the bottom and paste your External JS.
    7. Finally run command npm run dev in your terminal.

    your scripts will be updated and its compiled and moved to public/js/app.js . by adding a .js file in public folder will not effect on your page.


    If you want to add a External .css file..

    1. Copy your External styles.
    2. Run command npm install in your terminal.
    3. When the above command is executed you can see a folder named node_modules.
    4. Then go to resources/sass.
    5. Open file app.scss.
    6. Scroll down to the bottom and paste your External Styles.
    7. Finally run command npm run dev in your terminal.

    your scripts will be updated and its compiled and moved to public/css/app.css . by adding a .css file in public folder will not effect on your page.

    • Easiest and the safest way to add your external .css and .js.

    Laravel JavaScript & CSS Scaffolding

    YouTube Compiling Assets

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

    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'

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

    Ok so I was able to figure out for the version 5.2. You will first need to create assets folder in your public folder then put css folder and all css files into it then link it like this :

    <link href="assets/css/bootstrap.min.css" rel="stylesheet">
    

    Hope that helps!

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

    At first, you have to put your .css or .js files in the public folder of your project. You may create subfolders to it and move the files into the subfolder. Then you have to do the followings

    <link rel="stylesheet" href="{{asset('css/your_css_file.css')}}">
    <script src="{{asset('js/your_js_file.js')}}"></script>
    

    In my example, I have created two subfolders called css and js. I put all of the .css and .js files in corresponding folders and use them where ever I want them. Thank you and hope this helps you.

    0 讨论(0)
  • 2020-11-30 19:29
    {{ HTML::style('yourcss.css') }}
    {{ HTML::script('yourjs.js') }}
    
    0 讨论(0)
  • 2020-11-30 19:34

    Laravel 5.5 comes with mix , the replacement for Elixir, the external css(sass) can be extracted to resources/assets/css(sass) and import to app.css(scss) or give the correct path to your node_module folder that contains the library and can be used as

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

    The same can be done for Javascript too .

    0 讨论(0)
提交回复
热议问题