Assets not referencing to public folder (Laravel)

后端 未结 4 1632
鱼传尺愫
鱼传尺愫 2020-12-06 10:59

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 th

相关标签:
4条回答
  • 2020-12-06 11:31

    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.

    0 讨论(0)
  • 2020-12-06 11:39

    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.

    0 讨论(0)
  • 2020-12-06 11:44

    Add in your env:

    ASSET_URL=public

    or

    ASSET_URL=public/

    or

    ASSET_URL=/public

    which one u need u can do it...

    0 讨论(0)
  • 2020-12-06 11:54
    <link href="{{ asset('/css/style.css') }}" rel="stylesheet">
    

    Try this

    0 讨论(0)
提交回复
热议问题