I am working on a project that requires a secure connection.
I can set the route, uri, asset to use \'https\' via:
Route::get(\'order/details/{id}\',
"2020 Update? Url::forceScheme was acting funky for me, but this worked liked a dime."
https
code snippet.
resolve(\Illuminate\Routing\UrlGenerator::class)->forceScheme('https');
app/providers/RouteServiceProvider.php
. /**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
resolve(\Illuminate\Routing\UrlGenerator::class)->forceScheme('https');
parent::boot();
}
php artisan route:clear && composer dumpautoload
to clear Laravel's cached routes and cached Service Providers.Using the following code in your .htaccess file automatically redirects visitors to the HTTPS version of your site:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I would prefer forceScheme
instead of doing it on a web server. So Laravel app should be responsible for it.
So right way is to add if statement inside boot
function in your app/Providers/AppServiceProvider.php
if (env('APP_ENV') === 'production') {
\Illuminate\Support\Facades\URL::forceScheme('https');
}
Tip: to prove that you have APP_ENV configured correctly. Go to your Linux server, type env
This was tested on Laravel 5, specifically 5.6.
Here are several ways. Choose most convenient.
Configure your web server to redirect all non-secure requests to https. Example of a nginx config:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
Set your environment variable APP_URL
using https:
APP_URL=https://example.com
Use helper secure_url() (Laravel5.6)
Add following string to AppServiceProvider::boot() method (for version 5.4+):
\Illuminate\Support\Facades\URL::forceScheme('https');
Update:
Implicitly setting scheme for route group (Laravel5.6):
Route::group(['scheme' => 'https'], function () {
// Route::get(...)->name(...);
});
I used this at the end of the web.php
or api.php
file and it worked perfectly:
URL::forceScheme('https');
public function boot()
{
if(config('app.debug')!=true) {
\URL::forceScheme('https');
}
}
in app/Providers/AppServiceProvider.php