20
votes

I try include my css or js files to project using:

{{ URL::asset('path to css') }}

or

{!! Html::style("path to css") !!}

or similar.

But i always got link from public directory. like so: http://localhost/projectroot/public/assets/style.css

Why that happens and how can i solve this?

Seems to i should move my assets into public ?

3
Inside your assets is it style.css? - aldrin27
its not a problem. assets dir by default outside public dir in laravel. - WebArtisan
What is the path where you save your style.css? - aldrin27
projectroot\resources\assets\style.css - WebArtisan
The assets folder in the resources directory is for files that need compilation, like views or LESS files. Compiled assets, or plain assets like CSS and JS files should be in public/assets/. - BrokenBinary

3 Answers

38
votes

use the helper asset('path/to/css')

e.g.

<link rel="stylesheet" type="text/css" href="{{ asset('assets/style.css') }}">

assuming that your assets/style.css is under public

12
votes

In the simplest form, assets means JavaScript, CSS, and images which lie directly under the public directory and are publicly accessible using a URL.

Laravel provides a helper function, asset(), which generates a URL for your assets. You can use this in blade syntax, e.g.

<script type="text/javascript" src="{ { asset('js/jquery.js') } }"></script>
3
votes

That's perfectly normal. Your assets should be in the public folder. If you look at the .htaccess file in there, you'll see this:

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

This ensures that files located under /public are served directly without involving Laravel while all other urls go through index.php.

Also, your server configuration should make sure only the /public folder is URL-mapped since sensitive info can be leaked via parent paths (e.g. config files) otherwise.