Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: getScheme does not return 'https'

From within a controller, I have been using the method getSchemeAndHttpHost in order to build the url of another controller.

Everything has been running smoothly, until I needed to go over https. All urls that I get with the getSchemeAndHttpHost function are still http. Doing some troubleshooting, it came out that the getScheme function always returns http, both when I request the page with https and with http.

Did I miss something ?

How can I know if I am over http or https from my controller ?

Edit - some more information

  1. I made sure to run the tests with a valid certificate.
  2. If I run the isSecure method in my Request object, I always get false.
like image 554
mika Avatar asked Oct 30 '15 12:10

mika


1 Answers

Short answer

Add the following line in the controller:

Request::setTrustedProxies(array($request->server->get('REMOTE_ADDR')));

Some more details

After some investigations, I discovered that this has to do with Proxies... If we check what the isSecure function is doing, we find this: the Request method isFromTrustedProxy is called to decide if true or false should be returned.

Once we know the issue has to do with proxies, the symfony documentation explains in details what we should do to trust proxies or how to manage a load balancer or a reverse proxy. In my case, as I am working with heroku, trusting the incoming ip was the way to go.

like image 54
mika Avatar answered Nov 03 '22 08:11

mika