Creating a Configuration File

Using OpenAvanti requires a little configuration. You may have noticed this if you’ve read the articles leading up to this one.

We recommend setting up a configuration file, such as config.php in your document root to store all this configuration code. This makes your configuration code reusable so you don’t have to rewrite it or copy and paste it to every PHP file. Of course, if you’re following our MVC style, you should only have one file: index.php (more on that later). But it’s still a good idea to keep application logic and configuration separate.

Here’s what our generic configuration file config.php looks like (combining the code from the previous articles) for Non-distributed Applications:

<?php   
    $sBasePath = realpath( "../" );
 
    $aIncludePath = explode( PATH_SEPARATOR, get_include_path() );
 
    $aIncludePath[] = "{$sBasePath}/php/openavanti";
    $aIncludePath[] = "{$sBasePath}/php/models";
    $aIncludePath[] = "{$sBasePath}/php/controllers";
    $aIncludePath[] = "{$sBasePath}/php/views";
    $aIncludePath[] = "{$sBasePath}/php";
    $aIncludePath[] = "{$sBasePath}/pages";
 
    set_include_path( implode( PATH_SEPARATOR, $aIncludePath ) );
 
 
    // Setup database credentials (see articles on databases)
 
 
    function __autoload( $sClassName )
    {
        $aIncludePath = explode( PATH_SEPARATOR, get_include_path() );
 
        $sClassFile = "class." . strtolower( $sClassName ) . ".php";
 
        foreach( $aIncludePath as $sPath )
        {
            if( file_exists( "{$sPath}/{$sClassFile}" ) )
            {
                require( "{$sPath}/{$sClassFile}" );
                return;
            }
        }
 
        throw new FileNotFoundException( "Class {$sClassName} not found" );
    }
?>