Symfony2配置app base url

时间:2013-03-20 10:46:26

标签: symfony config base-url

我如何获得此基本网址

http://localhost/Symfony/web/app_dev.php/

OR

http://localhost/Symfony/web/app.php/

http://localhost/Symfony/web/

(基本网址)

在symfony2配置文件中?无法在symfony网站上找到。

2 个答案:

答案 0 :(得分:0)

$protocol = stripos($_SERVER['SERVER_PROTOCOL'],'https') === true ? 'https://' : 'http://';

$hostName = $request->server->get('HTTP_HOST');
$baseDirectory = "";
if(!empty($this->container->get('router')->getContext()->getBaseUrl())){
    $baseDirectory = str_replace('app_dev.php', '', $this->container->get('router')->getContext()->getBaseUrl());
}
$rootUrl = $protocol.$hostName.$baseDirectory; 

答案 1 :(得分:-1)

嗯......检查一下:

$base = $this->getRequest()->getSchemeAndHttpHost();

或在最旧的版本中(我不记得何时更改):

$request = $this->getRequest();
$scheme = $request->getScheme();
$host = $request->getHttpHost();

$base = sprintf('%s://%s', $scheme, $host);