How to Generate Secure Https URL from Route in Laravel?

By Hardik Savani November 5, 2023 Category : Laravel

Hey Developer,

Now, let's see post of laravel generate secure url from route. This example will help you laravel generate secure https url. This tutorial will give you a simple example of laravel force to https secure url. This post will give you a simple example of laravel force redirect to https. Let's get started with laravel force https routes.

In this article, I'll guide you through the process of generating secure URLs from routes in Laravel. We'll achieve this by enforcing a redirection to HTTPS URLs using the URL::forceScheme('https') method. To implement this, we'll make changes in the AppServiceProvider.php file. Let's explore this straightforward solution.

You can easily apply the following code to your AppServiceProvider.php file. This modification will only affect production URLs, while local URLs will remain unchanged.

app/Providers/AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use Illuminate\Support\Facades\URL;

class AppServiceProvider extends ServiceProvider

{

/**

* Register any application services.

*/

public function register(): void

{

}

/**

* Bootstrap any application services.

*/

public function boot(): void

{

if(env('APP_ENV') !== 'local') {

URL::forceScheme('https');

}

}

}

Now, you can check from your end.

I hope it can help you...

Tags :
Shares