Laravel HTTPS routes

in .env file

FORCE_HTTPS=true

in app/Providers/AppServiceProvider.php

public function boot()
{
    if(env('FORCE_HTTPS',false)) { 
        URL::forceScheme('https');
    }
}

After 5.4 the method name is forceScheme

if you use 'forceSchema' will get an error

"Method Illuminate\Routing\UrlGenerator::forceSchema does not exist."

The @dekts response is correct, but the "right" place to put this kind of stuff is the "app/Providers/AppServiceProvider.php" on the boot method.

//file: app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\URL;

class AppServiceProvider extends ServiceProvider {

    public function boot()
    {
        if (app()->environment('remote')) {
            URL::forceScheme('https');
        }
    }

    ...
}

You can also add a new variable on your ".env" file, something like:

#file: .env
FORCE_HTTPS=true

And change the condition to

//file: app/Providers/AppServiceProvider.php
public function boot()
{
    if(env('FORCE_HTTPS',false)) { // Default value should be false for local server
        URL::forceScheme('https');
    }
}

Hope this help.

edit: corrected forceSchema to forceScheme as noted on the comments. ;)


I struggled with this for ages. My solution was to put the following in my routes.php file (you may prefer a different place for it)

I have also wrapped a conditional statement around this line, so it only applies to my remote configuration:

if (App::environment('remote')) {
    URL::forceSchema('https');
}

You can try to add in your AppServiceProvider in the boot method. here: app/Providers/AppServiceProvider.php

Or another way:

So I figured it out. It not that easy but in your AppServiceProvider.php you must add: $this->app['request']->server->set('HTTPS', true); in register method. And that is how I figured it out.

Btw. Setting APP_URL does nothing to HTTP side of your app, it's for artisan.

PS. CloudFlare HTTPS Rewrites are also good idea :)


Automatic detection http/https

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Request;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
      /* Use https links instead http links */
      if (Request::server('HTTP_X_FORWARDED_PROTO') == 'https')
      {
         URL::forceScheme('https');
      }
    }
}

Tags:

Php

Laravel 5