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. Whats up are using Wordpress for your site platform? I'm new to the blog world but I'm trying to get started and create my own. Do you need any html coding knowledge to make your own blog? Any help would be really appreciated!

    Reply
  2. Hey There. I found your weblog the use of msn. That is a really well written article. I will be sure to bookmark it and come back to learn more of your helpful information. Thanks for the post. I'll certainly comeback.

    Reply
  3. An interesting discussion is definitely worth comment. I think that you ought to publish more about this subject matter, it may not be a taboo matter but generally people don't speak about such subjects. To the next! Best wishes!!

    Reply
  4. You made some really good points there. I checked on the web to find out more about the issue and found most people will go along with your views on this web site.

    Reply
  5. I used to be able to find good information from your blog posts.

    Reply
  6. Aw, this was an extremely good post. Taking a few minutes and actual effort to create a top notch article… but what can I say… I procrastinate a lot and don't seem to get anything done.

    Reply