Phrame tutorial with smarty
Download The Source Here: Phrame Tutorial Download
Phrameis an MVC Framework for php. If you are not familiar withmvc, Here Are Some Nice Pointers:
PHP Architect 2003-05: An Introduction To The Model-View-Controller Pattern (You Can Get A Free Issue There)
Discussion
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 (eg, fetch the right document from the DB in a CMS). There should be no exceptions , EG Bootstrapping Code That Will Display Pages. All Content Should Be CREATED IN ACTION CLASSES.
Setup
Create your project folder (referredto as "project" subsequently). Download Phrame and put it into your project folder in a folder named phrame. DownloadSmartyand place it under project / smarty. In the smarty dir, create the folders "config", "cache "," Templates "and" templates_c "(Not Recommended for a production system!) Create a log directory Under Project Directory.
You Should Nowhave 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
Creating files
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 file
'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 *}
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);
?>