問(wèn)題描述
我花了一些時(shí)間試圖理解 baseUrl 是什么(從學(xué)習(xí) Zend Framework 開(kāi)始),但令人驚訝的是,對(duì)于這樣一個(gè)無(wú)處不在的實(shí)用程序,沒(méi)有一個(gè)專(zhuān)家"或博主甚至試圖定義 baseUrl 以便學(xué)習(xí)者一個(gè)想法它是關(guān)于什么的.他們都假設(shè)你知道它是什么,然后繼續(xù)推導(dǎo)它,每個(gè)人都用自己的方法得出自己的結(jié)果.從我目前閱讀的內(nèi)容來(lái)看:
有些人認(rèn)為它是 Homepage-Url,我自然會(huì)認(rèn)為它是(從名稱(chēng)中暗示的),可以通過(guò) $_SERVER["HTTP_HOST"]
或 $_SERVER["SERVER_NAME"]
但令人驚訝的是這些似乎是少數(shù).
有些人認(rèn)為這是一個(gè)當(dāng)前頁(yè)面,可以通過(guò) $_SERVER["REQUEST_URI"]
或 $_SERVER["PHP_SELF"]
附加到 server-name 來(lái)訪問(wèn)>
而其他人認(rèn)為它可以是上述任何內(nèi)容或任何 url(嗯,至少這是我得到的印象),這取決于用戶希望如何使用它.
所以有人可以準(zhǔn)確解釋 baseUrl 是什么,而不假設(shè)我也是專(zhuān)家"以及為什么我可能需要它.謝謝.
baseUrl 應(yīng)該在大多數(shù) Zend 項(xiàng)目上自動(dòng)設(shè)置,但在我的情況下顯然不是.即使我執(zhí)行 echo $this->baseUrl()
或 var_dump($this->baseUrl())
,我也一無(wú)所獲.所以我真的不知道這個(gè)實(shí)用程序是關(guān)于什么的.
為什么需要 baseUrl()
工具的簡(jiǎn)短答案是:
應(yīng)用的部署位置應(yīng)該是配置問(wèn)題,而不是核心應(yīng)用功能問(wèn)題
在許多 - 甚至大多數(shù) - 情況下,應(yīng)用程序的 base-url 將只是 $_SERVER['HTTP_HOST']
或 $_SERVER['SERVER_NAME']
>.
但情況并非總是如此.
最容易考慮的例子是具有常用頁(yè)面的標(biāo)準(zhǔn)網(wǎng)站:
http://example.com/
http://example.com/contact
http://example.com/about
//等
現(xiàn)在您編寫(xiě)(或購(gòu)買(mǎi)或下載為 OSS 包)一個(gè)要部署在 url 下的博客應(yīng)用程序:
http://example.com/blog
該博客可能有以下鏈接:
http://example.com/blog/post/my-post-slug
http://example.com/blog/categories/some-category
//etc
所有這些鏈接都位于 /blog
下.
博客應(yīng)用程序本身應(yīng)該只關(guān)注博客內(nèi)的鏈接/頁(yè)面/路由.盡管您的博客模板很可能包含返回站點(diǎn)其余部分的頁(yè)眉/頁(yè)腳鏈接,但核心博客應(yīng)用程序的其余部分不必了解您站點(diǎn)的其余部分.不知何故,博客應(yīng)用程序需要知道,當(dāng)他生成指向博客文章和博客類(lèi)別以及所有其他與博客相關(guān)的頁(yè)面的鏈接時(shí),它們都必須以 http://example.com/blog
為前綴.
值 $_SERVER['HTTP_HOST']
或 $_SERVER['SERVER_NAME']
不反映此環(huán)境/部署信息.使用 base-url 值配置 (!) 應(yīng)用程序并在生成鏈接時(shí)始終使用某種 baseUrl()
函數(shù)(使用此配置)是保持核心應(yīng)用程序功能專(zhuān)注的好方法自己的業(yè)務(wù),而不是外部部署因素.
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.
這篇關(guān)于什么是 baseUrl的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!