Force URL::to() and url() Using Https Laravel 5.2

Diego Cortés
Diego Cortés
September 2, 2016
Force URL::to() and url() Using Https Laravel 5.2

We will see how to force the routes created with the url() and URL::to() helpers to use HTTPS instead of HTTP in Laravel 5.2 without modifying all our views one by one, for this we will go to the file:

\App\Providers\AppServiceProvider.php

where within the boot() function we have to add the following code:

if (\App::environment('production')) {
    \URL::forceSchema('https');
}

In this case we are first checking if in our .env file we have set the environment as production, if this is the case it will force the use of HTTPS in the routes created with the url() and URL::to() helpers.

That's all, I hope it helps more than one

Tested in Laravel 5.1 and 5.2

Article information

Published: September 2, 2016
Category: Laravel Tutorials
Reading time: 5-8 minutes
Difficulty: Intermediate

Key tips

1

Take your time to understand each concept before moving on to the next one.

2

Practice the examples in your own development environment for better understanding.

3

Don't hesitate to review the additional resources mentioned in the article.

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Frequently Asked Questions

Categories

Page loaded in 22.81 ms