问题
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
- I made sure to run the tests with a valid certificate.
- If I run the isSecure method in my Request object, I always get false.
回答1:
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.
来源:https://stackoverflow.com/questions/33436177/symfony2-getscheme-does-not-return-https