12

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.

I tried putting them under public/js and public/packages to no avail, getting the following 404 errors in my page:

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 .htaccess file looks like for reference:

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

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

2 Answers 2

28

All assets should be placed in the public folder

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

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

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

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

Hope this helps!

Sign up to request clarification or add additional context in comments.

4 Comments

@Yani How can I point different servers in asset function? For example, I want to have asset('192.168.2.4/jscr.js') in development and asset('s.mysite/jscr.js') in production.
@user2356198 asset() is not designed to support external scripts from outside your project folder. In your case, I'd write a helper function (i.e myAsset("jscrs.js")) and define a environment variable in your .env file which will be different in local and production environments.
@Yani I mean I would use asset in master blade file, not in js file.
@user2356198 not sure what you mean by that. You're more than welcome to post a new question on StackOverflow with some of your code and send me a link to it so I can try and answer..
2

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

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

1 Comment

What if you need to include some specific .js files for some views and then other .js files for other views? How do you handle or organize that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.