'Assets not referencing to public folder (Laravel)
I have the public folder inside my laravel project and I have some js and css files inside it.
I'm using the asset function and even though it's referencing to the public folder, my files aren't loaded on the page.
I'm using this code to load (it's only one example, there are more files):
<link href="{{ asset('css/style.css') }}" rel="stylesheet">
And on the browser's console, I'm geting something like this:
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8000/css/style.css
Well, I tried to revert the last commit, but no success. Tried to change to URL::asset() function, nothing. Tried everything from the following link: http://laravel.io/forum/09-17-2014-problem-asset-not-point-to-public-folder?page=1 and success.
Please, a little help?
Thanks!
Solution 1:[1]
I was having same problem. This is due to moving of .htaccess file from public to root of the project in order to serve localhost/project
rather than localhost/project/laravel
. And needed to use public as well in the asset:
<link href="{{ asset('public/css/app.css') }}" rel="stylesheet">
Or, modify the asset function from /Illuminate/Foundation/helpers.php
if (! function_exists('asset')) {
/**
* Generate an asset path for the application.
*
* @param string $path
* @param bool $secure
* @return string
*/
function asset($path, $secure = null)
{
return app('url')->asset("public/".$path, $secure);
}
}
The preceding method is not good way. Anyway this would have been easier if there was config for setting asset path.
Solution 2:[2]
Add in your env
:
ASSET_URL=public
or
ASSET_URL=public/
or
ASSET_URL=/public
which one u need u can do it...
Solution 3:[3]
After you've savely and succesfully setup your .htaccess
(like this), your method will work as you would expect it to work:
<link href="{{ asset('css/style.css') }}" rel="stylesheet">
Be aware that all client-side URL requests (requests to images, JavaScript/CSS files, JSON) will be affected by the Rewrite Rules after you've setup your .htaccess
.
Also be aware that it might take a while after you see the changes. Cache might interfere with these changes. So, make sure you clear your Laravel cache:
In your Shell run the following commands individually:
php artisan cache:clear
php artisan route:cache
php artisan config:cache
php artisan view:clear
And make sure to disable caching temporarily in your browser settings:
In Google Chrome: Open Dev Tools > Open the Network tab > Check "Disable cache".
Other browsers: search online how to do that.
Solution 4:[4]
In my case in .env
I tried
ASSET_URL=public
with both side slash and alone, nothing helped along with all related SO answers, then opened my old project and saw in. This worked for me finally (next to clearing views&caches, of course)
ASSET_URL="${APP_URL}"
im talking about localhost side
Solution 5:[5]
<link href="{{ asset('/css/style.css') }}" rel="stylesheet">
Try this
Solution 6:[6]
Add this is in your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
It Will solve your problem. SURE
Solution 7:[7]
Since you have moved your .htaccess file to the root folder you have to change the assets url wherever you are using. The URL should be like below,
<link href="{{ asset('public/css/style.css') }}" rel="stylesheet">
Or, You can just set the ASSET_URL as 'public' in the .env file,
ASSET_URL=http://localhost/projectname/public
Solution 8:[8]
sorry for my english, I have same problem in my case working with mike42/escpos-php was because in xampp
I move to zlib.output_compression=on
the correct way is
zlib.output_compression=off
and assets work again
Solution 9:[9]
stlye.css is loading and working for me from public folder. Don't forget to add rel="stylesheet"
<link href="{{ asset('/css/style.css') }}" rel="stylesheet">
Solution 10:[10]
And make sure to disable caching temporarily in your browser settings:
In Google Chrome:
Open Dev Tools > Open the Network tab > Check "Disable cache".
Other browsers: search online how to do that.
This worked for me. Seems the previous load of CSS and JS is in cache and until it is released the new ones do not load
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow