Getting base URL in Yii 2

后端 未结 10 2583
孤独总比滥情好
孤独总比滥情好 2020-12-30 19:32

I am trying to get the base URL for the project in Yii 2 but it doesn\'t seem to work. According to this page you used to be able to do:

Yii::app()->getBa         


        
10条回答
  •  悲哀的现实
    2020-12-30 20:23

    To get base URL of application you should use yii\helpers\Url::base() method:

    use yii\helpers\Url;
    
    Url::base();         // /myapp
    Url::base(true);     // http(s)://example.com/myapp - depending on current schema
    Url::base('https');  // https://example.com/myapp
    Url::base('http');   // http://example.com/myapp
    Url::base('');       // //example.com/myapp
    

    Url::home() should NOT be used in this case. Application::$homeUrl uses base URL by default, but it could be easily changed (for example to https://example.com/myapp/home) so you should not rely on assumption that it will always return base URL. If there is a special Url::base() method to get base URL, then use it.

提交回复
热议问题