Ankit
Ankit

Reputation: 657

Laravel 5.7 - CSS and JS not loading using URL::asset

I am new in laravel. I setup laravel 5.7 on my local system and put js and css into public folder but when hitting my site url on browser, site is not loading any css and js.

Site structure :

app
bootstrap
config
database
public
 -css
 -js
 -images
resources
 -views
    -includes
    -layouts
    -pages
routes
tests
vendor
index.php
server.php
.env

Note : I have cut .htaccess and index.php files and put these on root to run site without public in path.

Here is how I am calling url : CSS :

{{ URL::asset('css/style.css') }}

JS :

{{ URL::asset('js/query-1.11.1.min.js') }}

When seeing source code, the url looks like this :

http://localhost/mysite/css/style.css

So can anybody help me out from this issue ?

Thanks in advance.

Upvotes: 4

Views: 21854

Answers (7)

pankaj
pankaj

Reputation: 1906

Add new line in .env file

ASSET_URL=public

i fixed this issue this way.

Upvotes: 0

Imam Mubin
Imam Mubin

Reputation: 294

create .htaccess on your root folder and write this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^localhost$ [NC]
    RewriteRule ^(.*)$ public/$1 [L,NC] 
</IfModule>

Upvotes: -1

Udhav Sarvaiya
Udhav Sarvaiya

Reputation: 10061

FOR JAVASCRIPT :

<script type="text/javascript" src="{{ URL::asset('js/query-1.11.1.min.js') }}"></script>

FOR CSS :

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

NOTE: This will work if your directory structure is like this: /public/css/style.css OR /public/js/query-1.11.1.min.js

Upvotes: 0

dhara gosai
dhara gosai

Reputation: 92

you have directly put your css and js so you have to try this assets href="{{ assets('')}}" function.

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

and also js

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

Upvotes: 0

Cl&#233;ment Baconnier
Cl&#233;ment Baconnier

Reputation: 6058

You have 3 possibilites

Edit : I forgot Homestead

Upvotes: 1

Sagar Gautam
Sagar Gautam

Reputation: 9369

The asset() helper prepends the base URL to the path you supply.

The base URL is the location of index.php (in this case: http://localhost/mysite/).

If you don't want index.php inside /public you will need to use public/ inside your asset path, like this:

asset("public/css/style.css")

Same for the js files, I hope you understand.

Upvotes: 6

Jasim Juwel
Jasim Juwel

Reputation: 766

When cut .htaccess and index.php files must give public folder as path. Try with this:

css
{{ URL::asset('public/css/style.css') }}
js
{{ URL::asset('public/js/query-1.11.1.min.js') }}

Upvotes: 0

Related Questions