Laravel 强制使用 https

背景

前段时间给自己的网站安装了 SSL 证书,启用了 https。然而 laravel 本身并没有任何改变,资源引用、路由跳转都还是走的 http。

Laravel 强制使用 https

Providers\AppServiceProviderboot() 方法中添加以下代码:

URL::forceScheme('https');

<?php

namespace App\Providers;

use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        if ($this->app->environment() !== 'local') {
            URL::forceScheme('https');
        }
    }

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

$this->app['request']->server->set('HTTPS', $this->app->environment() != 'local');

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        //让Laravel支持https,且区分本地
        $this->app['request']->server->set('HTTPS', $this->app->environment() != 'local');
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
       //
    }
}    
Responses
  1. My family members every time say that I am wasting my time here at net, however I know I am getting familiarity every day by reading thes good articles.

    Reply
  2. Hey There. I found your blog the usage of msn. That is a really smartly written article. I'll be sure to bookmark it and return to learn extra of your useful info. Thanks for the post. I'll certainly comeback.

    Reply
  3. Hey there! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done a extraordinary job!

    Reply
  4. It's going to be end of mine day, but before end I am reading this fantastic piece of writing to improve my know-how.

    Reply
  5. I was recommended this website by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my trouble. You're incredible! Thanks!

    Reply
  6. Thanks designed for sharing such a good opinion, paragraph is good, thats why i have read it completely

    Reply