无法以HTTPS协议加载CSS

时间:2019-02-17 04:52:53

标签: php laravel heroku

我使用Laravel创建了一个博客,但遇到了一个奇怪的问题。我将项目推送到Heroku(nepshare.herokuapp.com)。当我访问此URL时,我的网站已加载但没有CSS,并且每次刷新都会使其加载但失败。 最后,我将https://nepshare.herokuapp.com URL更改为http://nepshare.herokuapp.com(从HTTPS更改为HTTP),然后一切正常,仅将CSS加载到CSS中。如何在HTTPS协议中呈现所有CSS?以下是我的主要布局代码:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ config('app.name', 'Laravel') }}</title>

    <!-- Scripts -->
    <script src="{{ asset('js/app.js') }}" defer></script>

    <!-- Fonts -->
    <link rel="dns-prefetch" href="//fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet" type="text/css">

    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>
<body>

@include('inc.navbar')
<div class="container">
    @include('inc.messages')
    @yield('content')
</div>

<script src="/vendor/unisharp/laravel-ckeditor/ckeditor.js"></script>
<script>
    CKEDITOR.replace('article-ckeditor');
</script>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

secure_asset()

secure_asset函数使用HTTPS为资产生成URL:

$url = secure_asset('img/photo.jpg');

答案 1 :(得分:1)

在您的.env文件中定义新属性,

REDIRECT_HTTPS = true

在您的app/Providers/AppServiceProvider.php中添加它,

namespace App\Providers;

use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(UrlGenerator $url)
    {
        if(env('REDIRECT_HTTPS')) {
            $url->formatScheme('https');
        }
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        if(env('REDIRECT_HTTPS')) {
            $this->app['request']->server->set('HTTPS', true);
        }
    }
}

现在您可以使用了,

<script src="{{ asset('js/app.js') }}" defer></script>

或者您可以使用secure_asset()帮助函数,但是secure_asset()方法仅使用https:https://laravel.com/docs/5.1/helpers#method-secure-asset