javascript 和 css 文件在 laravel 项目中的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22875556/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:18:39  来源:igfitidea点击:

Where do javascript and css files go in a laravel project

laravellaravel-4

提问by theycallmemorty

Where should static javascript and css files in a Laravel 4 application. For the sake of my own understanding, I'm not interested in using any sort of assets or packages yet.

Laravel 4 应用程序中的静态 javascript 和 css 文件应该在哪里。为了我自己的理解,我对使用任何类型的资产或包都不感兴趣。

I tried putting them under public/jsand public/packagesto no avail, getting the following 404 errors in my page:

我试图把他们下public/jspublic/packages没有用,让我在页面下面的404错误:

GET http://localhost/~myuser/mytest/packages/test2.js 404 (Not Found)
GET http://localhost/~myuser/mytest/js/mytest.js 404 (Not Found)

Here is what my .htaccessfile looks like for reference:

这是我的.htaccess文件的外观以供参考:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /~myuser/mytest/public/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

回答by dev7

All assets should be placed in the publicfolder

所有资产都应放在public文件夹中

public/js
public/css
public/fonts
public/images

You can access the assets using the asset()method or the Laravel HTML helper methods HTML::scriptand HTML::style. i.e to include Javascript file:

您可以使用asset()方法或 Laravel HTML 辅助方法HTML::scriptHTML::style. 即包含 Javascript 文件:

 - <script src="{{ asset('js/yourfile.js') }}"></script>

 - {{ HTML::script('js/yourfile.js') }}

Hope this helps!

希望这可以帮助!

回答by Paresh Gami

Inside the laravel folder you got public folder put css and js files here and access like this

在 Laravel 文件夹中,您将获得 public 文件夹,将 css 和 js 文件放在这里并像这样访问

<!doctype html>
<html lang="en">
    <head>
        <title>{{$title}}</title>
        {{HTML::style('css/style.css')}}
    </head>
    <body>
        @include('layouts.nav')      
        @yield('content')
    </body>
</html>