在 Laravel 中使用 https 加载 Blade 资产 [英] Load Blade assets with https in Laravel

查看:40
本文介绍了在 Laravel 中使用 https 加载 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.

这篇关于在 Laravel 中使用 https 加载 Blade 资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆