php 如何在 Laravel 5 中包含外部 CSS 和 JS 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28104583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include External CSS and JS file in Laravel 5
提问by Mansoor Akhtar
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.
我正在使用 Laravel 5.0,从这个版本中删除了 Form 和 Html Helper,我不知道如何在我的头文件中包含外部 css 和 js 文件。目前我正在使用此代码。
<link rel="stylesheet" href="/css/bootstrap.min.css"> <!--- css file --->
<script type="text/javascript" src="/js/jquery.min.js"></script>
回答by Todor Todorov
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:
这里我js
在 laravel 的app/public
文件夹中有一个目录。我有一个jquery.js
文件。函数 URL::asset() 为您生成必要的 url。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!
已弃用,在 Laravel 5 中不起作用!
回答by Jay Dhameliya
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.
您可以将路径传递给 javascript。
{!! HTML::script('js/script.js') !!}
Add the following lines in the require section of composer.jsonfile and run composer update "illuminate/html": "5.*".
在composer.json文件的 require 部分添加以下几行 并运行 composer update "illuminate/html": "5.*"。
Register the service provider in config/app.phpby adding the following value into the providers array:
通过将以下值添加到 providers 数组中,在config/app.php 中注册服务提供者:
'Illuminate\Html\HtmlServiceProvider'
'Illuminate\Html\HtmlServiceProvider'
Register facades by adding these two lines in the aliases array:
通过在别名数组中添加这两行来注册外观:
'Form'=> 'Illuminate\Html\FormFacade',
'HTML'=> 'Illuminate\Html\HtmlFacade'
'Form'=> 'Illuminate\Html\FormFacade',
'HTML'=> 'Illuminate\Html\HtmlFacade'
回答by AkshayBandivadekar
In laravel 5.1,
在 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
资产文件夹位置在公用文件夹内的位置
回答by Doni
i use this way, don't forget to put your css file on public folder.
我使用这种方式,不要忘记将您的 css 文件放在公共文件夹中。
for example i put my bootstrap css on
例如我把我的引导 css 放在
"root/public/bootstrap/css/bootstrap.min.css"
to access from header:
从标题访问:
<link href="{{ asset('bootstrap/css/bootstrap.min.css') }}" rel="stylesheet" type="text/css" >
hope this help
希望这有帮助
回答by adhix11
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>
回答by Julian Camilleri
{{ HTML::style('yourcss.css') }}
{{ HTML::script('yourjs.js') }}
回答by Foysal Nibir
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
首先,您必须将您的.css
或.js
文件放在public
项目的文件夹中。您可以为其创建子文件夹并将文件移动到子文件夹中。那么你必须做到以下几点
<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.
在我的示例中,我创建了两个名为 css 和 js 的子文件夹。我将所有.css
和.js
文件放在相应的文件夹中,并在需要的地方使用它们。谢谢你,希望这对你有帮助。
回答by Anoop D
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
Laravel 5.5 自带 mix ,Elixir 的替代品,可以将外部 css(sass) 提取到 resources/assets/css(sass) 并导入到 app.css(scss) 或提供正确的路径到包含库,可以用作
<link rel="stylesheet" href="{{ mix('css/app.css') }}" >
<link rel="stylesheet" href="{{ mix('css/app.css') }}" >
The same can be done for Javascript too .
对 Javascript 也可以这样做。
回答by Chetan Chitte
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 :
好的,所以我能够找出 5.2 版。您首先需要在公共文件夹中创建资产文件夹,然后将 css 文件夹和所有 css 文件放入其中,然后像这样链接它:
<link href="assets/css/bootstrap.min.css" rel="stylesheet">
Hope that helps!
希望有帮助!
回答by miniPax
First you have to install the Illuminate Html helper class:
首先你必须安装 Illuminate Html helper 类:
composer require "illuminate/html":"5.0.*"
Next you need to open /config/app.php and update as follows:
接下来你需要打开/config/app.php并更新如下:
'providers' => [
...
Illuminate\Html\HtmlServiceProvider::class,
],
'aliases' => [
...
'Form' => Illuminate\Html\FormFacade::class,
'HTML' => Illuminate\Html\HtmlFacade::class,
],
To confirm it's working use the following command:
要确认它正在工作,请使用以下命令:
php artisan tinker
> Form::text('foo')
"<input name=\"foo\" type=\"text\">"
To include external css in you files, use the following syntax:
要在您的文件中包含外部 css,请使用以下语法:
{!! HTML::style('foo/bar.css') !!}