SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Non-Member
    Join Date
    Nov 2004
    Location
    Ithaca, NY
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Application directory structure, and absolute/relative links?

    I'm working on a pretty big project (a few hundred pages of varying character), and I'm not sure of the best way to organize my files.

    I'm thinking something like this:

    /lib/ -- my PHP classes, generally one per file
    /inc/ -- pieces of PHP code common to every page, like login form, header, menu, etc
    /func/ -- user-defined functions that don't necessarily belong in a given class
    /styles/ -- css files

    Then there will be a lot of other directories for my actual content.

    One simple thing I'm stuck on is the best way to handle links. I want to be able to refer to /lib/ from some file in /bugs/galleries/mayflies/whatever/ without having to remember how deep I am in the directory structure and put the ../../../... etc. I also want the same files to work both on my local testing machine and the live server, so I can't just do www.mydomain.com/inc/ or something.

    I've got a working way to do this now but it's not as flexible as I'd like and it feels kind of sloppy. What's the good way?

    Another question I have: I find that I have an awful lot of include/requires at the top of every file, and each one is different depending on the classes it uses. It's just kind of messy and confusing. What's the best way to organize those to minimize that hassle?

  2. #2
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use $_SERVER['DOCUMENT_ROOT'] . '/lib/' and $_SERVER['DOCUMENT_ROOT'] . '/inc/' etc.
    This way, it will work whether you are on your local testing server, or the real site, and you never have to wirry about where yoiu are in the directory hierarchy.

  3. #3
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the way i do it (might nt be the best way) is i have all the classes and everything i need in one forlder. then i have my config file(has all the configuration data about all the classes, so its easy to manage all classes from one file) and then an include filelike
    PHP Code:
    require_once('class.errorhandler.php');
    require_once(
    'class.db.php');
    require_once(
    'class.user.php');
    //etc. etc. etc. 
    now i need all these files in every files that's why i can include this file in everyfile with the relative path. i could also use absolute (which i would like to) but the problem with my current ISP is they don't allow absolute paths (funny but true).

    i found it easy to manage one include file than many then i can just change one include file and i am done. and the good part is i am using classes so it doesn't matter if i am using one class in a file or not(because as far as i think it doesn't take memory until i initialize the object. i have some doubts about it too which i would like to clear)

    hope i am being clear enough

  4. #4
    Non-Member
    Join Date
    Nov 2004
    Location
    Ithaca, NY
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks swdev, that sounds good. What's the best way to shorten that? It'd get a little unwieldy to do that with every link on every page...

    Would a global $s = $_SERVER['DOCUMENT_ROOT'] be a good idea, so then in all my links I could do something like "$s/gallery/mayflies/whatever"?

    Is there any way to make it so all "relative" links across my site are interpreted as being relative to the document root, so I wouldn't need to write it into the expression for every url?

  5. #5
    SitePoint Member jdk's Avatar
    Join Date
    Aug 2000
    Location
    New Jersey, USA
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would do something like this.

    Have a config.php file where you define all the absolute paths.

    PHP Code:
    <?php
     
    //config.php
     
    define('LIB_PATH','/absolute/path/to/lib/directory');
    define('INC_PATH','/absolute/path/to/inc/directory');
     
    //so on and so forth.
     
    ?>
    Now always include config.php in all your php files.
    So whenever you want to include a class you can do it like

    PHP Code:
    <?
     
    //somefile.php
     
    require('config.php');
     
    include(
    LIB_PATH '/' 'myclass.php');
     
    //rest of the code follows. 
     
    ?>
    I would suggest not to include ALL files everytime but load only the files which you actually need.

    JD
    ________________________________
    d. k. jariwala
    I blog at http://jdk.phpkid.org

  6. #6
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    relative paths are relative from the the file they are being included not from the root. that's what the problem is.

    indec.php
    root-root.php
    db-db.php

    index.php
    PHP Code:
    require('root/root.php'); 
    root.php
    PHP Code:
    require('../db/db.php');// its WRONG in this context

    require('db/db.php');//CORRECT because root.php at this very moment is being included from in index.php 
    so, its better to use absolute paths, and you don't have to worry how the paths are interpreted at that very moment.

  7. #7
    SitePoint Member jdk's Avatar
    Join Date
    Aug 2000
    Location
    New Jersey, USA
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Troutnut
    Thanks swdev, that sounds good. What's the best way to shorten that? It'd get a little unwieldy to do that with every link on every page...
    How many scripts you have? 50,100, 5000?

    Don't you use macros in your favourite editor? Don't you use a editor which has auto complete?

    A little bit extra effort towards READABLE code goes a long way!
    So please, oh please, do not use short names and other short cuts like this!

    JD
    ________________________________
    d. k. jariwala
    I blog at http://jdk.phpkid.org


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •