php 什么是 baseUrl

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

What exactly is a baseUrl

phpzend-frameworkbase-url

提问by okey_on

I have spent sometime trying to understand what a baseUrl is(from learning Zend Framework), but it's surprising that for such a ubiquitous utility, not a single "expert" or blogger has even attempted to define baseUrl so that a learner has an idea what it is about. They all assume you know what it is and then proceed to derive it, each one employing his own method to arrive at his own result. From what I'd read so far:

我花了一些时间试图理解 baseUrl 是什么(从学习 Zend 框架中),但令人惊讶的是,对于这样一个无处不在的实用程序,没有一个“专家”或博主甚至试图定义 baseUrl 以便学习者知道什么是它是关于。他们都假设你知道它是什么,然后继续推导它,每个人都用自己的方法得出自己的结果。从我到目前为止读到的:

Some think it is a Homepage-Url, which is what I'd naturally think it is(as implied from the name), to be accessed by $_SERVER["HTTP_HOST"]or $_SERVER["SERVER_NAME"]but surprisingly these seem to be in the minority.

有些人认为它是一个主页-Url,我自然会认为它是(正如名称所暗示的),可以被访问,$_SERVER["HTTP_HOST"]或者$_SERVER["SERVER_NAME"]令人惊讶的是,这些似乎是少数。

Some think it is a current page to be accessed by $_SERVER["REQUEST_URI"]or $_SERVER["PHP_SELF"]appended to server-name

有些人认为这是一个当前页面,可以被server-name访问$_SERVER["REQUEST_URI"]$_SERVER["PHP_SELF"]附加到 server-name

while others think it can be any of the above or any url for that matter(well, at least that's the impression I get), depending on how the user wants to use it.

而其他人认为它可以是上述任何内容或任何 url(嗯,至少这是我得到的印象),这取决于用户想要如何使用它。

So can someone please explain exactly what a baseUrl is, without assuming I'm also "expert" and why I might need it. Thank you.

因此,有人可以请准确解释 baseUrl 是什么,而不假设我也是“专家”以及为什么我可能需要它。谢谢你。

EDIT:The baseUrl is supposed to be automatically set on most Zend projects, but apparently not in my case. Even when I do an echo $this->baseUrl()or var_dump($this->baseUrl()), I get nothing. So I really have no idea what this utility is about.

编辑:baseUrl 应该在大多数 Zend 项目中自动设置,但在我的情况下显然不是。即使我执行echo $this->baseUrl()or var_dump($this->baseUrl()),我也一无所获。所以我真的不知道这个实用程序是关于什么的。

回答by David Weinraub

The short answer for why you need to have a baseUrl()facility is this:

为什么您需要拥有baseUrl()设施的简短答案是:

Where the app is deployed should be a configuration issue, not a core application-functionality issue

应用程序的部署位置应该是配置问题,而不是核心应用程序功能问题

In many - perhaps even most - cases, the base-url of the application will simply be $_SERVER['HTTP_HOST']or $_SERVER['SERVER_NAME'].

在许多 - 甚至大多数 - 情况下,应用程序的 base-url 将只是$_SERVER['HTTP_HOST']or $_SERVER['SERVER_NAME']

But that's not always the case.

但情况并非总是如此。

The easiest example to consider is a standard website that has the usual pages:

最容易考虑的例子是一个标准网站,它有通常的页面:

  • http://example.com/
  • http://example.com/contact
  • http://example.com/about
  • // etc
  • http://example.com/
  • http://example.com/contact
  • http://example.com/about
  • // etc

Now you write (or buy or download as an OSS package) a blog application that you would like to deploy under the url:

现在您编写(或购买或下载为 OSS 包)一个要部署在 url 下的博客应用程序:

http://example.com/blog

http://example.com/blog

The blog could have links like:

该博客可能有如下链接:

  • http://example.com/blog/post/my-post-slug
  • http://example.com/blog/categories/some-category
  • //etc
  • http://example.com/blog/post/my-post-slug
  • http://example.com/blog/categories/some-category
  • //etc

All these links reside under /blog.

所有这些链接都位于/blog.

The blog application itself should be concerned onlywith links/pages/routing inside the blog. Though your blog templates may very well contain header/footer links back to the rest of the site, the rest of the core blog application functionality should not have to know about of the rest of your site. Somehow, the blog application needs to know that when he generates links to blog posts and blog categories and all his other blog-related pages, they all must be prefixed with http://example.com/blog.

该博客应用本身应该关注的只有的链接/网页/博客的内部路由。尽管您的博客模板很可能包含返回站点其余部分的页眉/页脚链接,但核心博客应用程序功能的其余部分不必了解您站点的其余部分。不知何故,博客应用程序需要知道,当他生成指向博客文章和博客类别以及所有其他博客相关页面的链接时,它们都必须以http://example.com/blog.

The values $_SERVER['HTTP_HOST']or $_SERVER['SERVER_NAME']do not reflect this environment/deployment information. Configuring (!) the app with a base-url value and consistently using some kind of baseUrl()function (that consumes this config) when generating links is a good way to keep your core application functionality focused on its own business and not on external deployment factors.

$_SERVER['HTTP_HOST']$_SERVER['SERVER_NAME']不反映此环境/部署信息。使用 base-url 值配置(!)应用程序并baseUrl()在生成链接时始终使用某种功能(使用此配置)是使您的核心应用程序功能专注于自己的业务而不是外部部署因素的好方法。

回答by Roopendra

Your web site is made up a series of web pages. Each web page has a full Uniform Resource Locator (URL) something like http://www.your.com/intro.htm. Your web page URLs will usually start with the same set of letters, eg http://www.your.com/in this case. This common prefix is what we call your Base URL.

您的网站由一系列网页组成。每个网页都有一个完整的统一资源定位符 (URL),类似于http://www.your.com/intro.htm。您的网页 URL 通常以相同的一组字母开头,例如在这种情况下为http://www.your.com/。这个通用前缀就是我们所说的基本 URL。

Using Base URLs simplifies your work as you do not need to see or type in the full URL for each page that you want to refer to.

使用基本 URL 可以简化您的工作,因为您无需查看或输入要引用的每个页面的完整 URL。

Or

或者

A URL assigned to a page to convert "relative URLs" into "absolute URLs" by the addition of a document name or trailing slash /

分配给页面的 URL,通过添加文档名称或尾部斜杠 / 将“相对 URL”转换为“绝对 URL”

To understand zend url please refer this link

要了解 Zend 网址,请参阅此链接