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