在 Laravel 中使用 https 加载 Blade 资产

2021-12-26 00:00:00 http https php laravel laravel-blade

我正在使用这种格式加载我的 css:<link href="{{ asset('assets/mdi/css/materialdesignicons.min.css') }}" media="all" rel="stylesheet" type="text/css"/>它可以为所有 http 请求加载良好

但是当我使用 SSL (https) 加载我的登录页面时,我得到一个 ...page... 是通过 HTTPS 加载的,但请求了一个不安全的样式表 'http...

谁能告诉我如何通过 https 而不是 http 加载刀片资源?

我应该尝试安全地加载资产吗?或者这不是Blade的工作?

解决方案

我在网站使用 HTTPS 时通过 HTTP 协议加载资源时 asset 功能出现问题,导致混合内容"问题.

要解决此问题,您需要将 URL::forceScheme('https') 添加到您的 AppServiceProvider 文件中.

所以我的看起来像这样(Laravel 5.4):

当您只在服务器上需要 https (config('app.env') === 'production') 而不是本地时,这很有用,所以不需要强制 资产函数使用https.

I am loading my css using this format: <link href="{{ asset('assets/mdi/css/materialdesignicons.min.css') }}" media="all" rel="stylesheet" type="text/css" /> and it loads fine for all http requests

But when I load my login page with SSL (https), I get a ...page... was loaded over HTTPS, but requested an insecure stylesheet 'http...

Can someone please tell me how do I make blade load assets over https instead of http?

Should I be trying to load the assets securely? Or is it not Blade's job?

解决方案

I had a problem with asset function when it's loaded resources through HTTP protocol when the website was using HTTPS, which is caused the "Mixed content" problem.

To fix that you need to add URL::forceScheme('https') into your AppServiceProvider file.

So mine looks like this (Laravel 5.4):

<?php

namespace AppProviders;

use IlluminateSupportServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        if(config('app.env') === 'production') {
            URL::forceScheme('https');
        }
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

This is helpful when you need https only on server (config('app.env') === 'production') and not locally, so don't need to force asset function to use https.

相关文章