Laravel强制使用HTTPS

2024-05-01 09:58:39.633416 +0800 CST 2 min read views: 307

可以在 RouteServiceProvider 类的 boot 方法中使用 forceScheme 方法来实现。

<?php

namespace App\Providers;

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

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot()
    {
        if (!App::environment('local')) {
            URL::forceScheme('https');
        }
    }

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