Skip to content

Instantly share code, notes, and snippets.

@zhanang19
Created July 22, 2020 23:49
Show Gist options
  • Save zhanang19/fd6a0f09531cc560f051171ff49035b5 to your computer and use it in GitHub Desktop.
Save zhanang19/fd6a0f09531cc560f051171ff49035b5 to your computer and use it in GitHub Desktop.
Force url generator in Laravel to use HTTPS scheme

Force url generator to use HTTPS

  • Make sure you're using this properly. Do at your own risk!
  • Add this service provider to your app
  • Adjust configuration in config/app.php like this:
    return [
        'https' => env('HTTPS', false),
    
        'providers' => [
            // Application Service Providers...
            App\Providers\ForceHttpsServiceProvider::class,
        ]
    ];
<?php
namespace App\Providers;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\ServiceProvider;
class ForceHttpsServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*/
public function boot(UrlGenerator $url)
{
if (config('app.https')) {
$url->forceScheme('https');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment