Yii2: Yii::$app->getRequest()->serverName check of isset

Created on 21 Dec 2015  ·  7Comments  ·  Source: yiisoft/yii2

In my application I need get domain name. I get it from request by means of next command :

Yii::$app->getRequest()->serverName

But when I run functional tests I get Exeption, because the created crawler doesn't have variable $_SERVER['SERVER_NAME']. This is content of $_SERVER :

{
  'HTTP_HOST' =>  string(9) "localhost"
  'HTTP_USER_AGENT' =>  string(19) "Symfony2 BrowserKit"
  'HTTPS' =>  bool(true)
  'REQUEST_URI' =>  string(15) "/index-test.php"
  'REQUEST_METHOD' =>  string(3) "GET"
}

Many parameters of class Request have check of isset :
https://github.com/yiisoft/yii2/blob/master/framework/web/Request.php#L842
https://github.com/yiisoft/yii2/blob/master/framework/web/Request.php#L851
https://github.com/yiisoft/yii2/blob/master/framework/web/Request.php#L869

But getServerName doesn't have it.
Sorry for my bad English )

enhancement

Most helpful comment

@NabiKAZ doesn't matter.

All 7 comments

Php checks server_name variable for existing, I suppose.

?

Зачем делать проверку поля, если оно по умолчанию забивается самим php?

В первом посте написано же: https://github.com/yiisoft/yii2/issues/10451#issue-123325703

@samdark
is better Yii::$app->getRequest()->serverName or Yii::$app->request->serverName ?

@NabiKAZ doesn't matter.

Yii::$app->request->hostInfo

Was this page helpful?
0 / 5 - 0 ratings