Skip to content
Advertisement

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!

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement