从控制器中的URL获取协议

Ben*_*der 6 symfony

我用谷歌搜索它,但也许我的搜索关键字没用,所以如何在控制器中获取URL协议?是http://或https://

http://whatever.com/app_dev.php/welcome

//I need to echo http:// protocol here
echo $request->getHost(); //echos whatever.com
echo $request->getBaseUrl(); //echos app_dev.php/
Run Code Online (Sandbox Code Playgroud)

use*_*941 8

您可以在控制器中使用它:

$scheme = $this->getRequest()->getScheme();
Run Code Online (Sandbox Code Playgroud)

否则,这是一个代码,允许您知道服务器返回的所有值:

 foreach ($_SERVER as $key => $value) {
      echo $key.' => '.$value.'<br>';
    }
Run Code Online (Sandbox Code Playgroud)

  • 上面的`foreach`循环可以最小化为`print_r($ _ SERVER);` (2认同)

Tom*_*ski 5

检查请求是否被https使用:

$request->isSecure()
Run Code Online (Sandbox Code Playgroud)

检查文档以获取更多信息