...

View Full Version : Application config setup



tom123
10-27-2009, 09:36 PM
Hi

I have the folowwong directory strucure...


Admin
- App
- Controller
- Inc
- View
- Config
- application.php
- Public
- index.php

I have the following setup in my VHosts file....


<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

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

abduraooft
10-28-2009, 08:00 AM
You may better to use absolute paths in your include statements. Make use of named constants to simplify the string like

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

tom123
10-29-2009, 12:10 AM
Thanks for the reply abduraooft.

As you can see i have my document root set to :

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:

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

kbluhm
10-29-2009, 06:07 AM
index.php


<?php

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

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

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

define( 'INDEX_FILE', __FILE__ );

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

define( 'DIR_ROOT', dirname( INDEX_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.

tom123
11-23-2009, 09:46 PM
Thanks for the usefull constant suggestions kbluhm.

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

My Document Root is:


C:\football\admin\public

Therefore the following definition....


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?

kbluhm
11-24-2009, 02:35 PM
Well it was simply an example. Very easy to fix.


define( 'DIR_ROOT', dirname( INDEX_FILE ) . DS );
define( 'DIR_APP' , dirname( DIR_ROOT ) . DS . 'App' . DS );
define( 'DIR_CFG' , dirname( DIR_ROOT ) . DS . 'Config' . DS );

tom123
11-24-2009, 10:23 PM
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?

kbluhm
11-24-2009, 10:46 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum