building a set of defined constants for application paths

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthew Crouch

    building a set of defined constants for application paths

    Okay, I've come up with hacky workarounds every time I build an app, and
    now I just need some tips on how to do this correctly and efficiently.

    I usually have a head.inc and/or a constants.inc with definitions like
    (SITE_ROOT,$_SE RVER['SERVER_NAME']."/myApp);
    (VERSION,"0.6.2 ");
    (APP_ROOT, SITE_ROOT."/".VERSION);
    (SMARTY_DIR, APP_ROOT . "/smarty");
    (ADMIN_DIR, APP_ROOT . "/admin");

    I'm sure you get the idea, and I bet you have them too. Some of these go
    fine, but many of them are all screwed up, esp. any dealing with
    -CSS (the stylesheets never seem to get included until my fourth attempt
    at writing the URL)
    -javascript (same)
    -images (sometimes ... it seems like different servers treat the initial
    slash in src="/thingy.png" differently?)

    Anyway, I always run into these snags in the midst of some project, and
    I figure out a hacky thing to just make it work. Now I want to get
    input/tutorial links, etc. on how to do this right:

    -do src= (script and img), action= (in a form), and href= (in a normal
    "<a" link, and in a <link stylesheet) have different behaviors, or am I
    imagining that?
    -should I just build absolute paths for everything?

    All advice greatly appreciated. If there isn't a single, definitive
    tutorial, I might build one based on feedback here.

    Thanks.
  • Oli Filth

    #2
    Re: building a set of defined constants for application paths

    Matthew Crouch said the following on 09/09/2005 14:48:[color=blue]
    > Okay, I've come up with hacky workarounds every time I build an app, and
    > now I just need some tips on how to do this correctly and efficiently.
    >
    > I usually have a head.inc and/or a constants.inc with definitions like
    > (SITE_ROOT,$_SE RVER['SERVER_NAME']."/myApp);
    > (VERSION,"0.6.2 ");
    > (APP_ROOT, SITE_ROOT."/".VERSION);
    > (SMARTY_DIR, APP_ROOT . "/smarty");
    > (ADMIN_DIR, APP_ROOT . "/admin");
    >
    > I'm sure you get the idea, and I bet you have them too. Some of these go
    > fine, but many of them are all screwed up, esp. any dealing with
    > -CSS (the stylesheets never seem to get included until my fourth attempt
    > at writing the URL)
    > -javascript (same)
    > -images (sometimes ... it seems like different servers treat the initial
    > slash in src="/thingy.png" differently?)[/color]

    Assuming you're talking about URLs in the HTML, then the server will
    never see something of the form "/thingy.png". Your browser sees it and
    converts it to an absolute URL before sending a request to the server.

    The basic rules:

    * Anything starting "http://" et al. is an absolute URL.

    * An initial slash in a relative URL means "work from the domain root",
    i.e. "/thingy.png" translates to "http://example.com/thingy.png",
    regardless of what the current path is.

    * Anything else is treated as relative to the path of the *current* page
    as seen by the browser.

    * "./" means "use the parent folder", so if you're at
    http://example.com/foo/bar/index.html, then "./thingy.png", translates
    to "http://example.com/foo/thingy.png".

    This is described formally in RFC 1808.

    [color=blue]
    > Anyway, I always run into these snags in the midst of some project, and
    > I figure out a hacky thing to just make it work. Now I want to get
    > input/tutorial links, etc. on how to do this right:
    >
    > -do src= (script and img), action= (in a form), and href= (in a normal
    > "<a" link, and in a <link stylesheet) have different behaviors, or am I
    > imagining that?[/color]

    No, they should all behave the same.

    [color=blue]
    > -should I just build absolute paths for everything?[/color]

    I would recommend not. It takes up more bandwidth to send (i.e. the HTML
    file is bigger), makes the HTML harder to read (for humans, that is!),
    and if you ever change domain or the root folder, then you'll have to
    change all your links (unless you build the links dynamically).


    --
    Oli

    Comment

    • John Dunlop

      #3
      Re: building a set of defined constants for application paths

      Matthew Crouch wrote:
      [color=blue]
      > If there isn't a single, definitive tutorial,[/color]

      Tutorial it is not, but RFC3986 (sec. 5) is the authoritative source
      for relative-ref resolution.



      --
      Jock

      Comment

      Working...