Skip to end of metadata
Go to start of metadata

<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 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

Zend_View_Helper_BaseUrl is a simple view helper that retrieves the base URL from Zend_Controller_Front.

2. References

3. Component Requirements, Constraints, and Acceptance Criteria

  • This component will retrieve the base URL from Zend_Controller_Front

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 prototype checked 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/.

9. Class Skeletons

]]></ac:plain-text-body></ac:macro>

]]></ac:plain-text-body></ac:macro>

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Sep 19, 2008

    <p>+</p>

  2. Oct 08, 2008

    <p>I think this should be added. I would guess almost everyone has some variant of this in all of their projects.</p>

  3. Oct 08, 2008

    <p>Even though this is really simple to do yourself, having a builtin helper would be neat.</p>

  4. Oct 09, 2008

    <p>+1</p>

  5. Oct 19, 2008

    <p>+1</p>

  6. Oct 20, 2008

    <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>

    1. Jan 30, 2009

      <p>That doesn't belong in a baseUrl helper, which is for fetching the base URL from Zend_Controller_Front.</p>

  7. Nov 10, 2008

    <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>

    1. Jan 30, 2009

      <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>

      1. Mar 26, 2009

        <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>

  8. Mar 07, 2009

    <p>I was always using an almost similar implementation in all of my projects. +1 from me for porting this to ZF.</p>

  9. Mar 27, 2009

    <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>

  10. Apr 29, 2009

    <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>

    1. Apr 29, 2009

      <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>

  11. 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>

  12. Jun 30, 2009

    <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>