Mgorunuch
Mgorunuch

Reputation: 686

Laravel HTTPS routes

today I decided to move my website to HTTPS. Early my website work on HTTP.

My problem is in misunderstood how Laravel pass HTTP and https in helpers function route('name')

I change my website URL in config/app.php to https://www.domain.name and I think, this solution helps me. But I got a strange result.

In php artisan tinker if I pass route('ROUTE.NAME') I got right link https://www.domain.name/route/path but in blade template I got http://www.domain.name/route/path

The same situation with \URL::to('/')

Maybe someone can explain to me why this happened?

Upvotes: 18

Views: 31367

Answers (5)

GaurangKumar
GaurangKumar

Reputation: 29

.env file

FORCE_HTTPS=true

app/Providers/AppServiceProvider.php

use Illuminate\Support\Facades\URL;
if (app()->environment('remote') || env('FORCE_HTTPS',false)) {
    URL::forceScheme('https');
}

Upvotes: 1

Leles
Leles

Reputation: 473

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. ;)

Upvotes: 36

Maxim
Maxim

Reputation: 71

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');
      }
    }
}

Upvotes: 6

Yasser
Yasser

Reputation: 1369

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."

Upvotes: 7

dekts
dekts

Reputation: 820

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 :)

Upvotes: 8

Related Questions