What exactly is a baseUrl

时光毁灭记忆、已成空白 提交于 2019-12-18 11:18:18

问题


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.


回答1:


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.




回答2:


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.

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.

Or

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

To understand zend url please refer this link



来源:https://stackoverflow.com/questions/14996129/what-exactly-is-a-baseurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!