简体   繁体   English

Yii2-获取不带参数的应用程序URL

[英]Yii2 - Get an app URL without params

I have no idea how to get a full url to my app web folder in Yii2. 我不知道如何在Yii2中获取完整的URL到我的应用程序Web文件夹。 The following rules: 以下规则:

<?=Yii::$app->getUrlManager()->getBaseUrl();?><br>
<?=Yii::$app->homeUrl;?><br>
<?=Yii::$app->getHomeUrl();?><br>
<?=Yii::$app->request->url;?><br>
<?=Yii::$app->request->absoluteUrl;?><br>
<?=Yii::$app->request->baseUrl;?><br>
<?=Yii::$app->request->scriptUrl;?><br>
<?=Url::to();?><br>
<?=Url::to(['site/index']);?><br>
<?=Url::base();?><br>
<?=Url::home();?><br>
<?=Yii::$app->getUrlManager()->getBaseUrl();?><br>

returns: 收益:

/yiiapp/web
/yiiapp/web/
/yiiapp/web/
/yiiapp/web/en/reset-password-request
http://website.com/yiiapp/web/en/reset-password-request
/yiiapp/web
/yiiapp/web/index.php
/yiiapp/web/en/reset-password-request
/yiiapp/web/site/index
/yiiapp/web
/yiiapp/web/
/yiiapp/web

when I need to get the (absoluteUrl is the closest one here): 当我需要获取时(absoluteUrl是最接近的此处):

http://website.com/yiiapp/web

I could probably combine one of the results with some $_SERVER var… but is it a solution? 我可以将其中一个结果与$ _SERVER var组合起来……但这是解决方案吗?

I realize this post is quite old but I want to answer it anyway. 我知道这篇文章已经很老了,但我还是想回答。 To get a full URL to your app web folder in Yii2 you can try these three options: 要在Yii2中获得指向您的应用程序Web文件夹的完整URL,您可以尝试以下三个选项:

  1. Url::to('@web/', ''); returns //website.com/yiiapp/web/ 返回//website.com/yiiapp/web/
  2. Url::to('@web/', true); returns http://website.com/yiiapp/web/ 返回http://website.com/yiiapp/web/
  3. Url::to('@web/', 'https'); returns https://website.com/yiiapp/web/ 返回https://website.com/yiiapp/web/

You can use Yii::$app->getUrlManager()->createAbsoluteUrl() method or yii\\helpers\\Url::toRoute() to generate absolute urls. 您可以使用Yii::$app->getUrlManager()->createAbsoluteUrl()方法或Yii::$app->getUrlManager()->createAbsoluteUrl() yii\\helpers\\Url::toRoute()生成绝对网址。 yii\\helpers\\Url::to() also can be used look at the documentation. yii\\helpers\\Url::to()也可用于查看文档。 Eg <?=Url::to(['site/index'], true);?> should output http://website.com/yiiapp/web/site/index . 例如<?=Url::to(['site/index'], true);?>应该输出http://website.com/yiiapp/web/site/index If you need to get root url to your app, try \\yii\\helpers\\Url::to('/', true); 如果您需要获取应用程序的根URL,请尝试\\yii\\helpers\\Url::to('/', true);

There are multiple ways to achieve this, but probably the most clean way to get base URL of your app is to use Url::base() : 有多种方法可以实现此目的,但是获取应用程序基本URL的最干净的方法可能是使用Url::base()

Url::base(true);

Most of methods in Url helper allows to you specify $scheme argument - you should use it if you want to create absolute URL (with domain). Url帮助程序中的大多数方法都允许您指定$scheme参数-如果要创建绝对URL(使用域),则应使用它。

The URI scheme to use in the returned base URL: 在返回的基本URL中使用的URI方案:

  • false (default): returning the base URL without host info. (默认):返回不带主机信息的基本URL。
  • true : returning an absolute base URL whose scheme is the same as that in yii\\web\\UrlManager::$hostInfo. true :返回一个绝对基本URL,该URL的方案与yii \\ web \\ UrlManager :: $ hostInfo中的方案相同。
  • string : returning an absolute base URL with the specified scheme (either http , https or empty string for protocol-relative URL). string :返回具有指定方案的绝对基本URL(对于协议相对URL,为httphttps或空字符串)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM