php HTTPS协议加载CSS失败

iszxjhcz  于 12个月前  发布在  PHP
关注(0)|答案(2)|浏览(114)

我用Laravel创建了一个博客,我遇到了一个奇怪的问题。(nepshare.herokuapp.com).当我访问这个网址我的网站加载,但没有CSS和我刷新每次使它加载,但失败.最后,我将https://nepshare.herokuapp.com URL更改为http://nepshare.herokuapp.com(从HTTPS改为HTTP)然后一切都很好。css只在HTTP中加载。如何在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>

字符串

5cg8jx4n

5cg8jx4n1#

在.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

sigwle7e

sigwle7e2#

您可以使用secure_asset辅助函数而不是asset来解决此问题。secure_asset函数使用HTTPS为资产生成URL。

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

字符串

相关问题