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. Thank you a lot for sharing this with all folks you actually recognise what you are speaking about! Bookmarked. Please also visit my site =). We can have a hyperlink trade contract between us

    Reply
  2. Hello friends, its impressive piece of writing about teachingand fully explained, keep it up all the time.

    Reply
  3. Hello! I could have sworn I've been to this site before but after browsing through some of the post I realized it's new to me. Anyways, I'm definitely happy I found it and I'll be book-marking and checking back frequently!

    Reply
  4. Hi, the whole thing is going well here and ofcourse every one is sharing facts, that's actually good, keep up writing.

    Reply
  5. Thanks for ones marvelous posting! I truly enjoyed reading it, you may be a great author.I will be sure to bookmark your blog and definitely will come back someday. I want to encourage you to ultimately continue your great job, have a nice evening!

    Reply
  6. Keep on working, great job!

    Reply