YII 2获取网站URL

Tou*_*afi 8 yii2 yii-url-manager

我的应用程序部署在localhost/upload上.

我使用以下代码生成相对URL.

 Url::to('@web/my_controller/action'); // it returns /upload/my_controller/action
Run Code Online (Sandbox Code Playgroud)

但是,我需要这样的完整URL:http:// localhost/upload/my_controller/action.

我错过了什么吗?

soj*_*oju 7

你应该简单地使用一个路线:

Url::to(['my_controller/action']);
Run Code Online (Sandbox Code Playgroud)

如果你想要一个绝对的基本网址:

Url::to(['my_controller/action'], true);
Run Code Online (Sandbox Code Playgroud)

阅读更多 :

http://www.yiiframework.com/doc-2.0/yii-helpers-baseurl.html#to()-detail

http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#creating-urls

  • 最后我想通了,我们只需要在url之后传递true,例如:`url:to(['my_controller/action'],true)`它将返回http://example.com/my_controller/action. (5认同)