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