Download the source here:
Phrame Tutorial download
Phrame
is an MVC framework for PHP. If you are not familiar with
MVC, here are some nice pointers:
php architect 2003-05: An introduction to the Model-View-Controller Pattern
(you can get a free issue there)
What I like MVC’ed web applications to be is simply this: Define Action classes for every use case, define templates for them, and then give this configuration to your controller and the work is done! For this example, I wanted to write one action class and share it across several ‘pages’. The action class is able to detect for which ‘page’ it was called and can act accordingly (e.g., fetch the right document from the DB in a CMS). There should be no exceptions, e.g. bootstrapping code that will display pages. All content should be created in action classes.
You should now
have this directory layout:
/phrame/
Action.php
ActionController.php
[...]
/phrame/util/
ArrayList.php
HashMap.php
/smarty/
core/
cache/
config/
templates/
templates_c/
Smarty_Compiler.class.php
Smarty.class.php
[...]
/log
Create the following files under your project directory:
action/MainAction.php
<?php
/**
* Main Action.
*/
class MainAction extends Action
{
function perform($actionMapping, $actionForm)
{
global $smarty;
$smarty->assign('action', $_REQUEST[_ACTION]);
$smarty->clear_cache('index.tpl');
$smarty->display('index.tpl');
//phpinfo();
return null;
}
}
?>
config/config.php
<?php
/**
* Application configuration.
*/
$__CFG = array (
'smarty_templates' => BASE . '/smarty/templates/',
'smarty_templates_c' => BASE . '/smarty/templates_c/',
'smarty_cache' => BASE . '/smarty/cache/',
'smarty_config' => BASE . '/smarty/config/',
// where to write log files
'log' => BASE . '/log/debug.log',
// the root of this application
// (use an empty string if it runs on top of a domain)
// example: use 'dir/' if this app is installed under
// example.com/dir
// you can also use the domain name, like
// 'http://example.com'
'root' => '',
);
?>
config/core.php
<?php
/**
* Core definitions that can be shared across apps.
*/
/**
* Define the base path.
*/
define('BASE', $_SERVER["DOCUMENT_ROOT"]);
/**
* Define smarty include path.
*/
define('SMARTY_DIR', BASE . '/smarty/');
?>
config/error.php
<?php
/**
* Error handling.
*/
set_error_handler('handleError');
/*
* This is the application error handler.
*
* @access public
* @param string $number
* @param string $message
* @param string $file
* @param string $line
* @param string $context
*/
function handleError($number, $message, $file, $line, $context)
{
global $__CFG;
switch($number) {
case E_ERROR:
$log = 'E_ERROR ';
break;
case E_WARNING:
$log = 'E_WARNING ';
break;
case E_PARSE:
$log = 'E_PARSE ';
break;
case E_NOTICE:
$log = 'E_NOTICE ';
break;
case E_CORE_ERROR:
$log = 'E_CORE_ERROR ';
break;
case E_CORE_WARNING:
$log = 'E_CORE_WARNING ';
break;
case E_COMPILE_ERROR:
$log = 'E_COMPILE_ERROR ';
break;
case E_COMPILE_WARNING:
$log = 'E_COMPILE_WARNING ';
break;
case E_USER_ERROR:
$log = 'E_USER_ERROR ';
break;
case E_USER_WARNING:
$log = 'E_USER_WARNING ';
break;
case E_USER_NOTICE:
$log = 'E_USER_NOTICE ';
break;
case E_STRICT:
$log = 'E_STRICT ';
break;
default:
$log = 'ERROR ';
}
$log .= strftime('%Y-%m-%d %H:%M:%S ', time());
$log .= "line $line in $file: '$message': ";
if (is_array($context)) {
//$log .= print_r($context, true);
$log .= "n";
} else {
$log .= "'$context'n";
}
error_log($log, 3, $__CFG['log']);
}
?>
config/mapping.php
<?php
/**
* Phrame mapping.
*/
// build mapping information to pass into controller
$__MAPPING = array(
_ACTION_FORMS => array(
'stdform' => array(
_TYPE => 'ActionForm'
)
),
_ACTION_MAPPINGS => array(
'main' => array(
_TYPE => 'MainAction',
_NAME => 'stdform',
_INPUT => 'index.php',
_VALIDATE => 0,
),
'blub' => array(
_TYPE => 'MainAction',
_NAME => 'stdform',
_INPUT => 'index.php',
_VALIDATE => 0,
),
'test' => array(
_TYPE => 'MainAction',
_NAME => 'stdform',
_INPUT => 'index.php',
_VALIDATE => 0,
),
)
);
?>
config/Options.php
<?php
/**
* Phrame options.
*/
//set options for the controller
$__OPTIONS = array(
_CACHE => 0,
//set to E_ALL to get controller errors (debug use only)
_ERROR_REPORTING => E_ALL,
//_ERROR_REPORTING => E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE,
_ERROR_HANDLER => "handleError",
);
?>
config/smarty.php
<?php
/**
* Automatically creates a smarty environment.
*/
require_once(SMARTY_DIR . '/Smarty.class.php');
define('SMARTY', 'smarty');
$smarty = new Smarty;
$smarty->template_dir = $__CFG['smarty_templates'];
$smarty->compile_dir = $__CFG['smarty_templates_c'];
$smarty->config_dir = $__CFG['smarty_config'];
$smarty->cache_dir = $__CFG['smarty_cache'];
$smarty->assign('base', BASE);
$smarty->assign('root', $__CFG['root']);
?>
smarty/templates/index.tpl
{* Smarty *}
<h1>Hello, this is {$action}!</h1>
<a href="{$root}/index.php">Index</a>
<a href="{$root}/index.php?action=blub">Blub</a>
<a href="{$root}/index.php?action=test">Test</a>
index.php
<?php
require_once('config/core.php');
require_once('config/config.php');
require_once('config/error.php');
require_once(BASE . '/phrame/include.php');
require_once('config/mapping.php');
require_once('config/options.php');
require_once('config/smarty.php');
require_once('actions/MainAction.php');
session_start();
trigger_error('blub');
// add controller to session if not already cached
if (!$_SESSION[_CONTROLLER])
{
$controller = new ActionController($__OPTIONS);
$_SESSION[_CONTROLLER] = $controller;
}
// put default action into session
if (!array_key_exists(_ACTION, $_REQUEST)) {
$_REQUEST[_ACTION] = 'main';
}
// release control to controller for further processing
$controller = &$_SESSION[_CONTROLLER];
$controller->process($__MAPPING, $_REQUEST);
?>
September 13th, 2007 at 11:32 am
I have visited your site 802-times
September 13th, 2007 at 11:33 am
Your site found in Google: http://google.com/search?q=hlw
September 13th, 2007 at 11:33 am
I could not find this site in the Search Engines index
July 24th, 2009 at 10:32 am
Link to phrame doesn’t work anymore. Moved to sourceforge
you can contact me here if you need help?