Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ireland
    Posts
    122
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Application config setup

    Hi

    I have the folowwong directory strucure...

    Code:
    Admin
      - App
        - Controller
        - Inc
        - View
      - Config
        - application.php
      - Public
        - index.php
    I have the following setup in my VHosts file....

    Code:
    <VirtualHost *:80>
      DocumentRoot "C:\football\admin\public"
      ServerName football
      DirectoryIndex index.php index.html
      <Directory "C:\football\admin\public">
        AllowOverride All
        Allow from all
      </Directory>
    </VirtualHost>
    And my index.php file has the folowing...

    PHP Code:
    <?php
        
        
    // Common application settings
        
    require_once('/admin/config/config.php');
        
        
    // Determine which page to process
        
    require('controller/contentSwitch.php');    
            
        
    // Construct page
        
    require('view/common/wrapper.php');
    ?>

    What im having trouble with is how to go about setting up a safe and clean config file to store all necessary paths and constants. Also the best way to setup and access info from this config file.

    Thanks in advance

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    You may better to use absolute paths in your include statements. Make use of named constants to simplify the string like
    PHP Code:
    <?php
        define
    (_ROOT_,$_SERVER['DOCUMENT_ROOT']);
        
    // Common application settings
        
    require_once(_ROOT_.'/admin/config/config.php');
        
        
    // Determine which page to process
        
    require(_ROOT_.'/controller/contentSwitch.php');  // Not sure if this is the right path  
            
        // Construct page
        
    require(_ROOT_.'/view/common/wrapper.php');
    ?>
    In that way, you can easily include any files from nay other file, provided the scope of the above definition is there.
    Last edited by abduraooft; 10-28-2009 at 08:03 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ireland
    Posts
    122
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks for the reply abduraooft.

    As you can see i have my document root set to :
    Code:
     DocumentRoot "C:\football\admin\public"
    Is it not good security practice to have just my index.php file inside the public folder accessable? I.e. Not to have my document root one level back where all app,config,etc.... would be accessible?

    I ask this as implementing the code you suggested would mean having to change my document root to:
    Code:
     DocumentRoot "C:\football\admin"
    I'm a bit of a newbie when it comes to setting up directory structures and how they are accessed and view.

    Thanks

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    index.php
    PHP Code:
    <?php

    /* - - - - - - - - - -
     * Separator constants
     */

    define'DS'DIRECTORY_SEPARATOR );
    define'PS'PATH_SEPARATOR );

    /* - - - - - - - - - -
     * File path constants
     */

    define'INDEX_FILE'__FILE__ );

    /* - - - - - - - - - -
     * Folder path constants
     */

    define'DIR_ROOT'dirnameINDEX_FILE ) . DS );
    define'DIR_APP' DIR_ROOT 'App'      DS );
    define'DIR_CFG' DIR_ROOT 'Config'   DS );

    /* - - - - - - - - - -
     * Bring everything together
     */

    // Common application settings
    require DIR_CFG 'config.php';

    // Determine which page to process
    require DIR_CFG 'Controller' DS 'contentSwitch.php';
        
    // Construct page
    require DIR_CFG 'View' DS 'common' DS 'wrapper.php';
    Now you know that DIR_APP and DIR_CFG are properly mapped to your /App and /Config folders, respectively. And since you're using the path to index.php as the root of all other constant values, your application can now reside anywhere so long as the app retains it's folder structure.
    Last edited by kbluhm; 10-29-2009 at 06:20 AM.

  • #5
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ireland
    Posts
    122
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks for the usefull constant suggestions kbluhm.

    There seems to be one problem with the solution you provided.

    My Document Root is:

    Code:
    C:\football\admin\public
    Therefore the following definition....

    Code:
    define( 'DIR_ROOT', dirname( INDEX_FILE ) . DS );
    define( 'DIR_APP' , DIR_ROOT . 'App'      . DS );
    define( 'DIR_CFG' , DIR_ROOT . 'Config'   . DS );
    ....will have the following values:
    admin/public
    admin/public/App/
    admin/public/Config/

    If you look at my origional directory structure, this new code seems to be wrong.

    Am I missing something here?

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Well it was simply an example. Very easy to fix.
    PHP Code:
    define'DIR_ROOT'dirnameINDEX_FILE ) . DS );
    define'DIR_APP' dirnameDIR_ROOT )   . DS 'App'    DS );
    define'DIR_CFG' dirnameDIR_ROOT )   . DS 'Config' DS ); 

  • #7
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ireland
    Posts
    122
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks kbluhm.

    Might it be cleaner and better to include all these Constants in a application setup file under Admin/Config?

    If so, is there a way I can incude the file using my Apache httpd or php.ini files?

  • #8
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Cleaner? Maybe, if you consider including an external file on one line to save 2 lines of code to be worth the 2 saved lines.

    Better? Not so much, as declaring the constants right there in the script incurs less overhead than including them in a separate file. Unless `cleaner` equals `better`, then it's only visually better by 2 lines.


  •  

    Posting Permissions

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