4

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.

4
  • 2
    Try with "{{ asset('css/custom.css') }}" Commented Dec 12, 2018 at 15:45
  • 1
    @Ankit If you are still in development mode, I don't see a reason why you hate public in the URL? You can make a virtual host on your machine and start coding. When you go live, you can just point your domain to public folder itself. So, you would never get public in your domain URL at all. Commented Dec 12, 2018 at 16:20
  • @vivek_23 Thanks for the suggestion. Now I again put .htaccess and index.php into public folder and now everything is working. Thanks once again. Commented Dec 12, 2018 at 16:31
  • @Ankit Welcome. Now, for security reasons, put all your folders except for public folder outside of your public_html and edit public/index.php with proper path for $app etc. Commented Dec 12, 2018 at 16:34

7 Answers 7

6

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.

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

7 Comments

This will work on local development, but not in production when he will remove public from url
@cbaconnier can you explain further ? please.
We can assume @Ankit as to browse localhost/mysite/public since index.php is also in public folder. On production you want to browse example.com not example.com/public That's why laravel provide multiple way to serve without public folder exposed in the url. See my answer
@cbaconnier thats good, I'm not too much familiar about production server. But, one thing is whats the problem for the url of the css and js files since there will no public keyword in the route we define in our routes ?
You point the domain example.com to your folder mysite/public where the index.php is located at. Aside that asset('public/css/... '); will generate the url example.com/public/css/... but Since your domain already point to public it will trow a 404 because your assets are in reality at the address example.com/css/...
|
1

You have 3 possibilites

Edit : I forgot Homestead

Comments

0

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') }}

Comments

0

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>

Comments

0

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

2 Comments

I tried this but it is not working. Actually I removed the .htaccess and index.php from public folder and put these files on root to run site without public.
You really shouldn't run your site from the root, this exposes your .env file
0

Add new line in .env file

ASSET_URL=public

i fixed this issue this way.

Comments

-1

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>

Comments

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.