美文网首页
Laravel 强制使用 https

Laravel 强制使用 https

作者: HongXunPan | 来源:发表于2020-02-22 14:32 被阅读0次

背景

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

Laravel 强制使用 https

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

  • 方法1
    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()
    {
       //
    }
}    

相关文章

网友评论

      本文标题:Laravel 强制使用 https

      本文链接:https://www.haomeiwen.com/subject/bjslqhtx.html