[SMARTY] Re: Is this a good way to setup Smarty?

Pedro <[email protected]>
Newsgroups gmane.comp.php.smarty.general
Message-ID <[email protected]>
Interesting setup you have as its similar to mine .. so I thought's I'd 
add variations so we can compare ..  for fun ;-)

This setup is for a local lan server as the test machine and a managed 
(ie we run it) host for production

Here's the way I would do it !

Karl Timmermann wrote:
> I saw this on the net, and was wondering if this is a good way up to  
> setup Smarty, DB, etc., and then each of my pages will require_once  the 
> attached file. Thanks!
> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> <?PHP
> /**
>  * set_env.php 
>  * @author Tom Anderson <[email protected]>
>  * @version 2.5
>  *
>  * This script sets up smarty, pear, and all other
>  * global settings for a php application.
>  */

// Site key needs to be  unquie for this project/web..
// locally this is the *nix user name for project
// this is used in $_SESSION[SITE_KEY]['variable'] for example so that
// the same value cannot be set somewhere else..
// eg two applications on the same web server using $_SESSION['user_id']
define('SITE_KEY','a_site');

// name of TEST_SERVER.
// This is used to switch on dev mode on local server/ off on production
define('TEST_SERVER', 'lan_test_server');

 > // db connect info
 > $dsn = 'mysql://root:root@localhost/database';


// create some path based for live vs production server
if($_SERVER['SERVER_NAME'] == TEST_SERVER){
	// dev server settings
	error_reporting(E_ALL);

	// above $dsn type was confusing to one member of my team !!
	$DB['type'] = 'mysql';
	$DB['host'] = 'localhost';
	$DB['user'] = 'root';
	$DB['pass'] = 'secret';
	$DB['db'] = 'dbatase';
	// dynamic constant cant be changed later in script
	define('WEB_ROOT', 'http://'.TEST_SERVER.'/~'.SITE_KEY.'/');
	// location of lib files .. local user account
	// - NO trailing slash
	define('LIBS_ROOT','/home/libs_user/public_html');
	// path to shared compile dir	 
define('SMARTY_COMPILE_DIR','/home/dev/smarty_shared_compile_dir/');

}else{
	error_reporting(E_ALL ^ E_NOTICE);
	$DB['type'] = 'mysql';
	$DB['host'] = '127.0.0.1';
	$DB['user'] = 'account';
	$DB['pass'] = 'anothersecret';
	$DB['db'] = 'account_database';
	// dynamic constant cant be changed later in script
	define('WEB_ROOT', 'http://www.mysite.org/subdirifneeded/');
	// libs directory of  ftp account
	// - NO trailing slash
	define('LIBS_ROOT','/home/ftp_libs_user');
	// path to shared compile dir	 
define('SMARTY_COMPILE_DIR','/home/smarty_shared_compile_dir/');

}

// path to smarty
define('SMARTY_DIR',LIBS_ROOT.'/Smarty-2.6.10/libs/');

> 
> // Setup include path
> $web_root = 'http://www.mysite.org/subdirifneeded/';


> $app_root = '/home/mysite/projects/myapp/';
// swaps windows file path to *nix flavour or current path
define('APP_ROOT', str_replace('\\','/',realpath('.')).'/' );

> # note:  sometimes pear is installed at /usr/local/lib/php/ but I 
> # recommend getting a copy that is local to your application so 
> # you can more easily controll when pear code is updated.
> $pear = $app_root . '/pear/'; 
$pear = APP_ROOT.'/pear/';

//I use ADODB so I'd do
define('ADODB_DIR',LIBS_ROOT.'/adodb464/');


> //---------------------------------------
> //--- end user edit
> 
> // Path to .ihtml (template) files
> define('TEMPLATE_DIR', "$app_root/ihtml/");
// I dont declare this an use templates/ by default


> $delim = (PHP_OS == "WIN32" || PHP_OS == "WINNT") ? ';': ':';
> ini_set('include_path', ".{$delim}$pear{$delim}$app_root/include{$delim}$app_root");
// dont like the includes path as it can be confusing
// to  developers who dont nderstand where things come form
> 
> // Set magic quotes based on database type.  If using either of these and using odbc, 
> // you will need to set this by hand.  
> // You should be able to safely comment this out if your system is setup right. 
> ini_set('magic_quotes_sybase', ($dbType == 'mssql' || $dbType == 'sybase') ? '1': '0');
> 
> // Include pear database handler, auth object (remove if not used), and smarty
> require_once 'DB.php';
require_once(LIBS_ROOT.'adodb.inc.php');
require_once(LIBS_ROOT.'adodb-errorhandler.inc.php');

> require_once 'smarty/Smarty.class.php';
require_once(SMARTY_DIR.'Smarty.class.php');
> 
> // Change error handling as necessary
> // PEAR_ERROR_RETURN, PEAR_ERROR_PRINT, PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE or PEAR_ERROR_CALLBACK
> // PEAR::setErrorHandling(PEAR_ERROR_PRINT);
> PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'errhndl');
> function errhndl ($err) {
>     echo '<pre>' . $err->message;
>     print_r($err);
>     die();
> } 
> 
> // Connect to the database and set fetch mode as necessary.  Include db extension as needed
> // The next two lines can be safely commented if your system is setup right.
> $extension = "php_$dbType" . (strpos(PHP_OS, "WIN") >= 0 ? ".dll" : ".so");
> if (!function_exists($dbType . '_connect')) dl($extension);
// nice touch .. like the above
// with $dbType swapped to $DB['type']
> 
> // Connect to the database
> $db = DB::connect($dsn);
> $db->setFetchMode(DB_FETCHMODE_ASSOC);  // Other modes possible; I find assoc best.
> $db->setOption('optimize', 'portability');  // This is really useful for me personally but may not be for everyone.
> 
// adodb flavour
$db = &ADONewConnection('mysql');
$db->PConnect($DB['host'],$DB['user'],$DB['pass'],$DB['db']);
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
unset($DB); // only paranoid survive

> // Setup template object - NOTE:  in this example, 'smarty' is a symlink to 
> // the smarty directory.  This allows you to upgrade Smarty without changing code.
> $t = new smarty;
$smarty = new Smarty(); // :-)
> $t->template_dir = TEMPLATE_DIR;
// i use the default templates/ dir
> // For other compile and cache directory options, see the comment by Pablo Veliz at the bottom of this article.
> $t->compile_dir = $app_root . '/compile';
$smarty->compile_dir = SMARTY_COMPILE_DIR;
> $t->cache_dir = $app_root . '/cache';
> // Because you should never touch smarty files, store your custom smarty functions, modifiers, etc. in /include
> $t->plugins_dir = array($app_root . '/include', $app_root . '/smarty/plugins');
// append other paths
// path to "stock" plugins created
$smarty->plugins_dir[] = LIBS_DIR.'/smarty_shared_plugins/';
// path to custom local plugins
$smarty->plugins_dir[] = APP_ROOT.'/libs/custom_smarties/';
> 
> // Change comment on these when you're done developing to improve performance
> $t->force_compile = true;
$smarty->force_compile = $_SERVER['SERVER_NAME'] == TEST_SERVER;

// additional settings
$smarty->debugging = $_SERVER['SERVER_NAME'] == TEST_SERVER;
$smarty->compile_id = SITE_KEY;
$smarty->use_sub_dirs = ini_get('safe_mode');
$smarty->compile_check 	= true;


interesting stuff ..

pete

> //$t->caching = true;
> 
> ## GLOBALS:  $db, $t
> session_start();
> 
> /* BEGIN USER AUTH - totally optional */
> // Add user authentication here if you want
> // This example uses pear::Auth
> require_once 'Auth/Auth.php';
> $a = new Auth(
> 	'DB',
> 	array(
> 		'table' => 'users',
> 		'usernamecol' => 'login',
> 		'passwordcol' => 'password',
> 		'dsn' => $dsn
> 		),
> 	'login_function_name',
> 	true
> );
> 
> // Use case insensitive login
> if (isset($_POST['username'])) $_POST['username'] = strtoupper($_POST['username']);
> 
> // Check for logout
> if ($_REQUEST['logout'] && $a->getAuth()) {
> 	$a->start();
> 	$a->logout();
> 	session_destroy();
> 	header("Location: $web_root");
> 	exit();
> }
> 
> // Init the session for this user and authorize.
> $a->start();
> // You could use a global '$require_login = 1;' before 'require_once 'set_env.php';' 
> // then check for it here so not all pages require login
> if (!$a->getAuth()) {
> 	die();
> }
> /* END USER AUTH */
> 
> // Assign any global smarty values here.
> $t->assign('web_root', $web_root);
> ?>
> 
> 
> ------------------------------------------------------------------------
> 
> 

-- 
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.