<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[
<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[
Zend_View_Helper_BaseUrl is a simple view helper that retrieves the base URL from Zend_Controller_Front.Zend Framework: Zend_View_Helper_BaseUrl Component Proposal
Proposed Component Name
Zend_View_Helper_BaseUrl
Developer Notes
http://framework.zend.com/wiki/display/ZFDEV/Zend_View_Helper_BaseUrl
Proposers
Geoffrey Tran
Robin Skoglund
Zend Liaison
Matthew Weier O'Phinney
Revision
1.1 - 10 September 2008: Revised Draft. (wiki revision: 10)
Table of Contents
1. Overview
2. References
3. Component Requirements, Constraints, and Acceptance Criteria
4. Dependencies on Other Framework Components
- Zend_Controller_Front
- Zend_View_Helper_Abstract
5. Theory of Operation
This component is used like any other view helper, and will return the base URL from Zend_Controller_Front. Optionally, a file path can be specified when calling the helper. This path will be appended to the returned base URL.
6. Milestones / Tasks
- Milestone 1:
Working prototypechecked into the incubator supporting use cases #1 and #2 - Milestone 2:
Unit tests exist, work, and are checked into SVN. - Milestone 3: Initial documentation exists.
7. Class Index
- Zend_View_Helper_BaseUrl
8. Use Cases
| UC-01: Print base URL directly in a view |
|---|
In this example, the base URL in Zend_Controller_Front is /mypage/.
| UC-02: Add a file path to the base URL |
|---|
This example shows how a file path can be added to the base URL. The base URL is still /mypage/.
16 Comments
comments.show.hideSep 19, 2008
Jason Eisenmenger
<p>+</p>
Oct 08, 2008
Ryan Mauger
<p>I think this should be added. I would guess almost everyone has some variant of this in all of their projects.</p>
Oct 08, 2008
Jani Hartikainen
<p>Even though this is really simple to do yourself, having a builtin helper would be neat.</p>
Oct 09, 2008
Sam Hauglustaine
<p>+1</p>
Oct 19, 2008
Timo Ziemann
<p>+1</p>
Oct 20, 2008
Lars Strojny
<p>I'm not sure if a simple base URL helper is good enough. What about managing different URLs for static content etc. pp. What about refactoring the current URL helper to support "categorized" URLs. So you create a link to - say - foo.jpg and tag the URL as "image". Than you can configure the URL helper to rewrite URLs tagged "image" as img.static.yourhost.com.</p>
Jan 30, 2009
Robin Skoglund
<p>That doesn't belong in a baseUrl helper, which is for fetching the base URL from Zend_Controller_Front.</p>
Nov 10, 2008
Hinikato Dubrai
<p>As said Lars Strojny baseUrl is not good enough. If Apache URL not used or user requested URL in form <a class="external-link" href="http://host/index.php">http://host/index.php</a> file, URL to images must be without index.php file. In this case should be used Zend_Controller_Request_Http::getBasePath() method to get valid URL.</p>
Jan 30, 2009
Robin Skoglund
<p>If you want to change the base URL, you do it in the front controller. If a user tries to access <a class="external-link" href="http://www.example.com/index.php/foo">http://www.example.com/index.php/foo</a>, then that is a valid URL, and should not be magically rewritten by the view helper. Let's not be Microsoft here.</p>
Mar 26, 2009
Jan Sorgalla
<p>I think what he meant is that (using your example) accessing <a class="external-link" href="http://www.example.com/folder/index.php">http://www.example.com/folder/index.php</a> would produce something like:</p>
<p><link rel="stylesheet" type="text/css" href="/folder/<strong>index.php</strong>/css/base.css" /></p>
Mar 07, 2009
Ben Scholzen
<p>I was always using an almost similar implementation in all of my projects. +1 from me for porting this to ZF.</p>
Mar 27, 2009
Matthew Weier O'Phinney
<ac:macro ac:name="note"><ac:parameter ac:name="title">Zend Acceptance</ac:parameter><ac:rich-text-body>
<p>This proposal is accepted for immediate development in the standard incubator, with the following criteria/potential additions:</p>
<ul>
<li>Should incorporate a setBaseUrl() method. This can be accessed using $view->getHelper('baseUrl')->setBaseUrl(), and could be used in cases where public resources may be served via a CDN instead of locally.</li>
<li>Please address the question of baseUrls that end in a filename</li>
<li>Consider adding module awareness to the helper, perhaps as a second argument. This could allow for namespacing public resources by module. (This is an optional requirement.)</li>
</ul>
</ac:rich-text-body></ac:macro>
Apr 29, 2009
daniel prieler
<p>it would also be nice to get an absolute url:</p>
<p>(e.g. for a facebook-app i had to use an absolute url for images)</p>
<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
function baseUrl($file = null, $absolute = false) {
....
if ($absolute)
$baseUrl = 'http://'.$request->getHttpHost().$baseUrl;
return $baseUrl;
}
]]></ac:plain-text-body></ac:macro>
Apr 29, 2009
Robin Skoglund
<p>You can use the baseUrl helper in combination with the serverUrl helper. For ease of use, you can make your own url helper that combines the two.</p>
Jun 26, 2009
fc
<p>The problem with some View helpers, including this one, is the reference to the front controller. I'm using the Zend_View package at the moment, but not the Zend_Controller one. What leaves me with 2 options: Remove this class from my build process, or fork it. </p>
<p>I'm sure you can get the base URL without calling the front controller. What are the advantages of doing it this way?</p>
Jun 30, 2009
Halil Özgür
<p>Would be very helpful addition indeed.</p>
<p>And I'm happy to see that bigger scale systems are being taken into account, like the CDN issue.</p>
<p>@Federico Cargnelutti<br />
Really, it would be pretty nice to have a static/independent base URL function. But I'm not sure if the job of getting the correct base URL in all of the environments and in all conditions is a trivial one. Still, part of that problem remains afloat even when getting it from the front controller.</p>