Welcome Guest, Not a member yet? Register   Sign In
Site library
#1

[eluser]dcheslow[/eluser]
It's nice to have site information in one place... stuff like company name, address, phone number, copyright, etc. Further, each site may have some number of hosts associated with it - only one of which is relevant at any given point in time. It's nice to be able to use EXACTLY the same code in development on localhost as on the testing server and the production server. I've found various ways of handling this... some people put the information in $GLOBALS, some put it in $config, some even put this stuff in a database table... none of those really met all my needs. So I came up with a simple Site class which I'll share here. By putting this class in a CI library I can put all the relevant information about the site in a config file. It looks like this:

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

$config['domainname']= 'WiseGarage.com';
$config['companyname']= 'WiseGarage.com';
$config['postal']=array(
        '3404 Browning Street',
        'Victoria, BC  V8P 4E1',
        'CANADA',
        );
$config['phone']= '(250) 888-4161';
$config['email']= '[email protected]';
$config['firstlaunched']= 2008;
$config['offsets']=array(
        'javascript'=>'system/application/assets/javascript/',
        'flash'=>'system/application/assets/flash/',
        'image'=>'system/application/assets/siteimages/',
        'css'=>'system/application/assets/css/',
        'assetcache'=>'system/application/assets/cache/',
        'upload'=>'system/application/uploads/',
        );
$config['hosts'] = array(
    'localhost' => array(
        'baseurl' =>'http://localhost:8888/wisegarage/',
        'basepath'=>'/Applications/MAMP/htdocs/wisegarage/',
        'gspath'=>'/usr/local/bin/gs',
        'debug'=> TRUE,
        'cacheassets'=>TRUE),
    '216.198.218.141' => array(
        'baseurl' =>'http://216.198.218.141/~wisegara/',
        'basepath'=>'/home/wisegara/public_html/',
        'gspath'=>'gs',
        'debug'=> FALSE,
        'cacheassets'=>TRUE),
    'wisegarage.com' => array(
        'baseurl' =>'http://wisegarage.com/~wisegara/',
        'basepath'=>'/home/wisegara/public_html/',
        'gspath'=>'gs',
        'debug'=> FALSE,
        'cacheassets'=>TRUE),
    );

?>

I then autoload this library:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Site
{
    protected $_ci;
    public $domainname= '';
    public $companyname= '';
    public $phone= '';
    public $email= '';
    public $postal='';
    public $paths=array();
    public $urls=array();
    public $debug = FALSE;
    public $cacheassets = FALSE;
    public $copyright = '';
    public $simplecopyright= '';
    public $myself = '';

    function __construct($config)
    {
        $this->_ci =& get_instance();
        // load up all the basics
        $this->domainname = $config['domainname'];
        $this->companyname = $config['companyname'];
        $this->phone = $config['phone'];
        $this->_ci->load->helper('url');
        $this->email = safe_mailto($config['email'],'Email '.$config['domainname']);
        // build the mailing address
        $this->postal = $this->companyname;
        foreach($config['postal'] as $thisaddressline)
        {
            $this->postal.='<br />'.$thisaddressline;
        }
        $this->postal.='<br />';
        // get the base url/path (and some other stuff) for whatever host we're using
        foreach($config['hosts'] as $thishost =>$hostconfig)
        {
              if(stristr($_SERVER['SERVER_NAME'],$thishost) !== FALSE)
            {
                foreach($hostconfig as $key=>$value)
                {
                    switch($key)
                    {
                        case 'debug':
                            $this->debug = $value;
                            break;
                        case 'baseurl':
                            $this->urls['base'] = $value;
                            break;
                        case 'basepath':
                            $this->paths['base'] = $value;
                            break;
                        case 'cacheassets':
                            $this->cacheassets = $value;
                            break;
                        default:
                            $this->paths[$key] = $value;            
                    }
                }
            }
        }
        // create all the other paths
        foreach($config['offsets'] as $thisoffsetname=>$thisoffset)
        {
            $this->paths[$thisoffsetname] = $this->paths['base'].$thisoffset;
            $this->urls[$thisoffsetname] = $this->urls['base'].$thisoffset;
        }
        $this->myself = $this->urls['base'].'index.php?/';

        // build the copyright notices
        $years='';
        $lastyear=(int) date('Y');
        for($thisyear=$config['firstlaunched'];$thisyear <= $lastyear; $thisyear++)
        {
            $years.=$thisyear;
            if($thisyear < $lastyear)
            {
                $years.=', ';
            }
        }
        $this->copyright = "Copyright {$years} &copy; all rights reserved by <a href='{$this->urls['base']}' >{$this->companyname}</a>";
        $this->simplecopyright = "Copyright {$years}, all rights reserved by {$this->companyname}";

    }

}
?&gt;

Now I can access information about the site using expressions like:

Code:
$this->site->companyname

and (more importantly)

Code:
$this->site->urls['image']

The class automagically figures out which server is being used and sets all the urls and paths accordingly. It even calculates a copyright notice that is always up-to-date ... sweet!

Hope someone finds this useful.

=dave=


Messages In This Thread
Site library - by El Forum - 04-24-2008, 05:09 PM
Site library - by El Forum - 04-25-2008, 12:44 AM
Site library - by El Forum - 04-25-2008, 02:02 AM
Site library - by El Forum - 04-25-2008, 02:10 AM
Site library - by El Forum - 04-25-2008, 02:35 AM
Site library - by El Forum - 04-25-2008, 03:44 PM
Site library - by El Forum - 04-25-2008, 04:47 PM
Site library - by El Forum - 04-26-2008, 12:40 AM
Site library - by El Forum - 04-26-2008, 11:32 PM
Site library - by El Forum - 04-27-2008, 01:04 AM
Site library - by El Forum - 04-27-2008, 01:18 AM



Theme © iAndrew 2016 - Forum software by © MyBB