Reputation: 4227
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 ?
Upvotes: 28
Views: 159958
Reputation: 210
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>
Upvotes: 18
Reputation: 549
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
Upvotes: 45
Reputation: 10380
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.
Upvotes: 5