Phrame Tutorial with Smarty

Phrame Tutorial with Smarty

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)

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 (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.

Setup

  • Create your project folder
    (referred
    to as "project" subsequently).
  • Download Phrame and put it into your project folder in a folder named phrame.
  • Download
    Smarty
    and 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 your project directory.

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

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 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);

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值