Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 assets links

Tags:

php

laravel

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 ?

like image 454
WebArtisan Avatar asked Sep 21 '15 22:09

WebArtisan


3 Answers

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

like image 133
Joseph Dan Alinsug Avatar answered Nov 17 '22 09:11

Joseph Dan Alinsug


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>
like image 44
Deepak Soni Avatar answered Nov 17 '22 09:11

Deepak Soni


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.

like image 44
bernie Avatar answered Nov 17 '22 10:11

bernie