什么是 baseUrl [英] What exactly is a baseUrl

查看:79
本文介绍了什么是 baseUrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

有些人认为它是 Homepage-Url,我自然会认为它是(从名称中暗示的),可以通过 $_SERVER["HTTP_HOST"]$_SERVER["SERVER_NAME"] 但令人惊讶的是这些似乎是少数.

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

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

所以有人可以准确解释 baseUrl 是什么,而不假设我也是专家"以及为什么我可能需要它.谢谢.

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

解决方案

为什么需要 baseUrl() 工具的简短答案是:

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

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

但情况并非总是如此.

最容易考虑的例子是具有常用页面的标准网站:

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

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

http://example.com/blog

该博客可能有以下链接:

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

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

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

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

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:

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.

Some think it is a current page to be accessed by $_SERVER["REQUEST_URI"] or $_SERVER["PHP_SELF"] appended to 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.

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

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.

解决方案

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

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'].

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

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

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

All these links reside under /blog.

The blog application itself should be concerned only with 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.

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.

这篇关于什么是 baseUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆