Home » Php » php – Laravel HTTPS routes

php – Laravel HTTPS routes

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&Answers:

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::forceSchema('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::forceSchema('https');
    }
}

Hope this help.

Answer:

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 🙂

Answer:

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

Answer:

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