How to force laravel to generate https link?

Member

by lew , in category: Third Party Scripts , a month ago

How to force laravel to generate https link?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by deron , a month ago

@lew 

To force Laravel to generate HTTPS links, you can set the forceScheme option in your appHttpMiddlewareTrustProxies.php middleware file.


You can add the following lines at the top of the middleware file:

1
use IlluminateHttpRequest;


Then, modify the handle method as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public function handle(Request $request, Closure $next)
{
    $request->setTrustedProxies([$request->getClientIp()], Request::HEADER_X_FORWARDED_ALL);

    if ($request->isSecure()) {
        URL::forceScheme('https');
    }

    return $next($request);
}


This code snippet will force Laravel to generate HTTPS links when the request is secure.