php 如何在 Yii 框架中设置 Base URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7613335/
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-26 03:02:27  来源:igfitidea点击:

How to Set Base URL in Yii Framework

phpyiiyii-routing

提问by David Xia

When I do

当我做

print_r(Yii::app()->request->baseUrl)

I get an empty string. A post on the Yii forum says this is blank by default. How can I change its default value so that I can use absolute URLs?

我得到一个空字符串。Yii 论坛上的一个帖子说默认情况下这是空白的。如何更改其默认值以便我可以使用绝对 URL?

回答by ecco

You can edit /path/to/application/protected/config/main.php to change the default value. Add a request component, and configure baseUrl porperty.

您可以编辑 /path/to/application/protected/config/main.php 以更改默认值。添加请求组件,并配置 baseUrl 属性。

return array(
    ...
    'components' => array(
        ...
        'request' => array(
            'baseUrl' => 'http://www.example.com',
        ),
    ),
);

回答by bool.dev

As the post in that forum says, it might be different for different platforms, or if the web app isnt located in the default folder.
All these things work for me:

正如该论坛中的帖子所说,对于不同的平台,或者网络应用程序未位于默认文件夹中,它可能会有所不同。
所有这些都对我有用:

echo Yii::app()->request->baseUrl."<br/>" ;
print_r(Yii::app()->request->baseUrl);
echo "<br/>";
var_dump(Yii::app()->getBaseUrl(true));
echo "<br/>";
echo Yii::app()->request->getBaseUrl(true);

I used yiic to create the web app, with default settings using the following command in a terminal, yiic webapp /path/to/webapp
So that generates the necessary directory structure for the web app, and also the default skeleton files. Try it and then see how it works. I'm new to yii myself.

我使用 yiic 创建了 web 应用程序,在终端中使用以下命令使用默认设置,yiic webapp /path/to/webapp
这样就为 web 应用程序生成了必要的目录结构,以及默认的骨架文件。尝试一下,然后看看它是如何工作的。我自己是 yii 的新手。

Edit:

编辑:

This solution might have worked for the op, but the correct way baseUrl can be set is shown by ecco's answer to this question.

此解决方案可能适用于该操作,但ecco 对此问题的回答显示了可以设置 baseUrl 的正确方法。

回答by ldg

It's most likely blank because your bootstrap file (index.php) is at your web root. If it wasn't, you should see some value. Changing it would defeat it's purpose.

它很可能是空白的,因为您的引导程序文件 (index.php) 位于您的 Web 根目录。如果不是,您应该会看到一些价值。改变它会破坏它的目的。

You would use like:

你会使用像:

href="<?php echo 'http://www.myhost.com' . Yii::app()->request->baseUrl; ?>/css/screen.css"

which for most cases wouldn't change the path, but if you did, say, decide to put your Yii app inside a subdirectory, then it would be portable. (Simply remove the http method and hostname above to make it work on the same host the user is on.)

在大多数情况下不会改变路径,但如果你这样做了,比如说,决定把你的 Yii 应用程序放在一个子目录中,那么它就会是可移植的。(只需删除上面的 http 方法和主机名,使其在用户所在的同一主机上工作。)

回答by balrok

Yii::app()->baseUrl returns just a relative Path from the url to index.php for example: 127.0.0.1/index.php returns '' 127.0.0.1/yii/index.php returns '/yii'

Yii::app()->baseUrl 只返回从 url 到 index.php 的相对路径,例如:127.0.0.1/index.php 返回 '' 127.0.0.1/yii/index.php 返回 '/yii'

回答by Sachin G.

If you are using Url helper to generate urls, you will need to set baseUrl key to UrlManager component

如果您使用 Url helper 生成 url,则需要将 baseUrl 键设置为 UrlManager 组件

 'urlManager' => [       
        'baseUrl' => 'http://example.com', 

Then you can create absolute url by using Url helper as,

然后你可以通过使用 Url helper 作为创建绝对 url,

echo Url::to(['site/index', 'src' => 'ref1', '#' => 'name'], true); // output ===> http://example.com/index.php?r=site%2Findex&src=ref1#name

echo Url::to(['site/index', 'src' => 'ref1', '#' => 'name'], true); // output ===> http://example.com/index.php?r=site%2Findex&src=ref1#name

回答by Kushagra

try echo Yii::$app->baseUrl(true)
Note that the true parameter is what does the needful.

尽量echo Yii::$app->baseUrl(true)
注意,真正的参数是什么呢要紧的。

回答by Anoop Saini

Using Yii 2.0.13.1the below code is working fine

使用 Yii 2.0.13.1下面的代码工作正常

<?php echo Yii::$app->request->baseUrl; ?>