php 在 Yii 2 中获取基本 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27856533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:34:13  来源:igfitidea点击:

Getting base URL in Yii 2

phpyiiyii2

提问by Brett

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

我试图在 Yii 2 中获取项目的基本 URL,但它似乎不起作用。根据此页面,您过去可以执行以下操作:

Yii::app()->getBaseUrl(true);

In Yii 1, but it seems that that methodin Yii 2 no longer accepts a parameter?

在 Yii 1 中,但似乎Yii 2中的该方法不再接受参数?

I've tried doing it without true, such as:

我试过在没有 true 的情况下这样做,例如:

Yii::$app->getBaseUrl();

But it just returns empty.

但它只是返回空。

How can you do this in Yii 2?

你怎么能在 Yii 2 中做到这一点?

回答by rob006

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

要获取应用程序的基本 URL,您应该使用yii\helpers\Url::base()方法:

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::$homeUrluses 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.

Url::home()在这种情况下不应使用Application::$homeUrl默认情况下使用基本 URL,但它可以轻松更改(例如更改为https://example.com/myapp/home),因此您不应依赖于它始终会返回基本 URL 的假设。如果有一种特殊的Url::base()方法来获取基本 URL,则使用它。

回答by DiegoCoderPlus

My guess is that you need to look at aliases.

我的猜测是您需要查看别名。

Using aliases would be like:

使用别名就像:

Yii::getAlias('@web');

You can also always rely on one of these two:

您也可以始终依赖以下两者之一:

Yii::$app->homeUrl;

Url::base();

回答by MrBii

To get base URL Yii2 using:

使用以下方法获取基本 URL Yii2:

Url::home(true)

回答by NovaLogic

Use it like this:

像这样使用它:

Yii::$app->getUrlManager()->getBaseUrl()

More information on base, canonical, home URLs: http://www.yiiframework.com/doc-2.0/yii-helpers-url.html

有关基本、规范、主页 URL 的更多信息:http: //www.yiiframework.com/doc-2.0/yii-helpers-url.html

回答by rahul s negi

may be you are looking for this

可能你正在寻找这个

Yii::$app->homeUrl

you can also use this

你也可以用这个

Url::base().

or this

或这个

Url::home();

Url::home();

回答by Radhe9254

Try this:

尝试这个:

$baseUrl = Yii::$app->urlManager->createAbsoluteUrl(['/']);

回答by Bira

I searched for a solution how we can do like in codeigniter, routing like e.g.

我搜索了一个解决方案,我们可以像在 codeigniter 中那样做,例如路由

base_url()
base_url('profile')
base_url('view/12')

Only way we can do that in Yii2

只有这样我们才能在 Yii2 中做到这一点

<?=Url::toRoute('/profile') ?>

回答by Thaier Alkhateeb

You can reach your base URL by this:

您可以通过以下方式访问您的基本网址:

Yii::$app->request->baseUrl

回答by Neeraj Maurya

Try below code. It should work. It will return base URL name

试试下面的代码。它应该工作。它将返回基本 URL 名称

use yii\helpers\Url;

使用 yii\helpers\Url;

Url::home('http') // http://HostName/OR Url::home('https') // https://HostName/

Url::home('http') // http://HostName/或 Url::home('https') // https://HostName/

回答by vasillis

In yii 1 this code return the hostname

在 yii 1 中,此代码返回主机名

Yii::app()->getBaseUrl(true);

In yii2 the following

在yii2以下

Yii::$app->getBaseUrl();

not exists as method of Yii::$app and it triggers an error with message

不存在作为 Yii::$app 的方法,它会触发一个带有消息的错误

Calling unknown method: yii\web\Application::getBaseUrl() 

You could use Request class that encapsulates the $_SERVER

您可以使用封装 $_SERVER 的 Request 类

Yii::$app->request->hostInfo