Getting Started with HTML_FlexyFramework, DataObjects and HTML_Template_Flexy
Published 2006-07-28 00:00:00
Since most of the applications I write, and get teams to write for me,
use the FlexyFramework, I though some simple instructions might be
usefull.
FlexyFramework, is an extremely simple pair of classes that help load action classes, do internal redirection, and load up basic configuration settings. Basically taking all the hard work out of setting up a reasonably secure web application.
In this simple example, I'm going to use a little project called FlexyCash, so if you are setting up a project, just replace all the references to FlexyCash with your project name.
You'll have to visit the site, as the actual tutorial is in the extended entry bit....
FlexyFramework, is an extremely simple pair of classes that help load action classes, do internal redirection, and load up basic configuration settings. Basically taking all the hard work out of setting up a reasonably secure web application.
In this simple example, I'm going to use a little project called FlexyCash, so if you are setting up a project, just replace all the references to FlexyCash with your project name.
You'll have to visit the site, as the actual tutorial is in the extended entry bit....
Step 1 - Make a Project directory.
create the project directory:mkdir /var/www/flexycash
Step 2 - Install pearball
download the pearball tgz or create one using the pearball creation script, and extract it into the project directorycd /var/www/flexycash
tar xvfz pearball-......tgz
Note: this pearball includes the FlexyFramework code..
Step 3 - Create bootstrap file
create a bootstrap/config file (eg. cash.php), or index.php I've not used index.php, as the project is stored in subversion, and when checked out, you can rename the file to index.php, and change to values to suit the installation.
See this file for an example:
<?php
// set up the include path to work! - our project, and our private pear install
ini_set('include_path',
realpath(dirname(__FILE__)) . ":"
. PATH_SEPARATOR . realpath(dirname(__FILE__)) . '/pear'
);
require_once 'HTML/FlexyFramework.php';
new HTML_FlexyFramework(array(
// all of these are available via HTML_FlexyFramework::get()->**section**
'project' => 'FlexyCash', // our project name
'FlexyCash' => array( // our project varoable
// put project options in here..
),
// set up the database connection + path to dataobjects.
'DB_DataObject' => array(
'database' => 'mysql://user:@localhost/gnucash', // fill in the correct details here!
'schema_location' => 'FlexyCash/DataObjects',
'class_location' => 'FlexyCash/DataObjects',
'require_prefix' => 'FlexyCash/DataObjects/',
'class_prefix' => 'FlexyCash_DataObjects_',
//'quote_identifiers' = 1
),
// any other stuff can go here..
));
Step 4 - Create Project base class
create the base class for the project.Things to remember here
- template should be defined (which template is shown when you go to the root page of the site)
- getAuth() should be defined, and if everyone is allowed to the homepage, just return true.
- get() and post() should be defined, and should check the incomming string, if it's not handled, then redirect to an error handler (or in our simple example, just exit.)
<?php
class FlexyCash extends HTML_FlexyFramework_Page
{
var $template = 'welcome.html';
function getAuth()
{
return true; // everyone is allowed here!
}
function get($str)
{
// if we get a url that is not handled throw a 404 error page.
if (strlen($str)) {
echo "404 - not allowed";
// HTML_FlexyFramework::run('Error/404');
exit;
}
}
function post($str)
{
// if we get a url that is not handled throw a 404 error page.
if (strlen($str)) {
echo "404 - not allowed";
// HTML_FlexyFramework::run('Error/404');
exit;
}
}
}
Step 5 - Create master template
create the master template: /var/www/flexycash/FlexyCash/templates/master.html- our simple example is this. - just put the header text in there, and the outputBody():h call.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>FlexyCash - {title}</title>
<style> </style>
</head>
<body>
{outputBody():h}
</body>
</html>
Step 6 - Create Welcome page
Create the welcome page in the file/var/www/flexycash/FlexyCash/templates/welcome.htmlagain with some simple contents
Hello World <BR/>
root= {rootURL} <BR/>
base= {baseURL}
Step 7 - Test it
Test if it's working - go to http://localhost/flexycash/cash.php
You may get this message - which tells you that magic quotes must be turned off!
magic quotes is enabled add the line
php_value magic_quotes_gpc 0
to your .htaccess file
(Apache has to be configured to "AllowOverride Options AuthConfig" for the directory)
If you are lucky, you get this message displayed.
Hello World
root= /flexycash
base= /flexycash/cash.php
Step 8 - Create the DataObjects
Assuming you have designed your database in Mysql Query Browser, or similar.create a file (somewhere outside of the web directory) eg.
/tmp/create_flexycash.ini
[DB_DataObject]run createTables on it.
database = mysql://user:@localhost/gnucash
schema_location = /var/www/flexycash/FlexyCash/DataObjects
class_location = /var/www/flexycash/FlexyCash/DataObjects
require_prefix = FlexyCash/DataObjects/
class_prefix = FlexyCash_DataObjects_
debug = 1
php -d include_path=/var/www/flexycash/pear \This should generate all the dataobjects for your project
/var/www/flexycash/pear/DB/DataObject/createTables.php /tmp/create_flexycash.ini
Step 9 - Create some code to use the database
In the base class, I have added a method loadAccounts()It loads all the accounts to be displayed as a tree on the right of the web page. -It's defined as a method here, so that other pages can call it in their get() methods.
Note: I dont have a constructor to do this automagically, as I like each Action class to be reasonably self contained when explaining how it builds the page.
Note: We also do the bulk of the work inside the extended dataobject, as this code may be usefull elsewhere in the future...
// from class FlexyCashCreate the code in the DataObject. - this creates a nested array of dataobjects showing the tree of the accounts. (only works on PHP5, as object now get passed by reference, rather than by copy)
// in get() method:
$this->loadAccounts();
var $accountsArray;
function loadAccounts()
{
$ac = DB_DataObject::factory('account');
$this->accountsArray = $ac->loadAccountsTree();
function loadAccountsTree()
{
$this->orderBy('name');
$this->find();
$ar = array();
while ($this->fetch()) {
if (!isset($parents[$this->parent])) {
$parents[$this->parent] = array();
}
$parents[$this->parent][] = clone($this);
}
foreach($parents[0] as $id => $obj) {
$this->buildTree($obj, $parents);
}
return $parents[0];
}
public $children;
function buildTree($obj, $parents)
{
$obj->children = !empty($parents[$obj->id]) ? $parents[$obj->id] : array();
foreach($obj->children as $id => $sobj) {
$this->buildTree($sobj, $parents);
}
}
Step 10 - add code in template to dump the tree.
Normally, I dont allow the PHP code to generate HTML, but tree's tend to be an exception, as there is no clean way of doing it purely using a template.So to dump the tree that we have built I changed the welcome template to use this:
<ul class="account-list">
<li flexy:foreach="accountsArray,account">
{account.toHtmlLi(baseURL):h}
</li>
</ul>
Step 11 - provide a method to dump tree nodes.
In the dataobject, we provide a simple recursive method to output the tree data with some urls.function toHtmlLi($baseurl)Now we can visit the home page again and see a list of the accounts as a unordered list.
{
$ret = '<a href="'. htmlspecialchars($baseurl). '/Account/'. $this->id . '.html">' .
htmlspecialchars($this->name) . '</a>';
if (empty($this->children)) {
return $ret;
}
$ret .= '<ul>';
foreach($this->children as $c) {
$ret .= '<li>'. $c->toHtmlLi($baseurl) . "</li>\n";
}
return $ret . '</ul>';
}
Mentioned By:
www.phpdeveloper.org : PHPDeveloper.org: Alan Knowles' Blog: Getting Started with HTML_FlexyFramework, DataObjects and HTML_Template_Flexy (206 referals)
www.nexen.net : Nexen.net : portail PHP et MySQL - Dmarrer avec le FlexyFramework (145 referals)
google.com : april (95 referals)
www.planet-php.net : Planet PHP (94 referals)
google.com : december (88 referals)
swik.net : PHP: Hypertext Preprocessor : Planet PHP : Getting Started with html_FlexyFramework, DataObjects and html_Template_Flexy - SWiK (54 referals)
google.com : html_flexyframework (48 referals)
google.com : html_template_flexy example (38 referals)
google.com : HTML_Template_Flexy (35 referals)
objectmix.com : Nested foreach-statements in templates - Application Development and Programming (24 referals)
google.com : FlexyFramework (17 referals)
www.phpdeveloper.org : PHPDeveloper.org: PHP News, Views, and Community (14 referals)
google.com : php flexy tutorial (11 referals)
www.nexen.net : Nexen.net : portail PHP et MySQL - Actualits (9 referals)
google.com : FlexyFramework download (9 referals)
google.com : HTML_Template_Flexy examples (9 referals)
www.thescripts.com : Nested foreach-statements in templates - PHP (8 referals)
planet.dsource.org : Planet D (8 referals)
google.com : php getting url (8 referals)
www.phpfreaks.com : PHP Help: Alan Knowles' Blog: Getting Started with HTML_FlexyFramework, DataObjects and HTML_Template_Flexy (7 referals)
www.phpdeveloper.org : PHPDeveloper.org: Alan Knowles' Blog: Getting Started with HTML_FlexyFramework, DataObjects and HTML_Template_Flexy (206 referals)
www.nexen.net : Nexen.net : portail PHP et MySQL - Dmarrer avec le FlexyFramework (145 referals)
google.com : april (95 referals)
www.planet-php.net : Planet PHP (94 referals)
google.com : december (88 referals)
swik.net : PHP: Hypertext Preprocessor : Planet PHP : Getting Started with html_FlexyFramework, DataObjects and html_Template_Flexy - SWiK (54 referals)
google.com : html_flexyframework (48 referals)
google.com : html_template_flexy example (38 referals)
google.com : HTML_Template_Flexy (35 referals)
objectmix.com : Nested foreach-statements in templates - Application Development and Programming (24 referals)
google.com : FlexyFramework (17 referals)
www.phpdeveloper.org : PHPDeveloper.org: PHP News, Views, and Community (14 referals)
google.com : php flexy tutorial (11 referals)
www.nexen.net : Nexen.net : portail PHP et MySQL - Actualits (9 referals)
google.com : FlexyFramework download (9 referals)
google.com : HTML_Template_Flexy examples (9 referals)
www.thescripts.com : Nested foreach-statements in templates - PHP (8 referals)
planet.dsource.org : Planet D (8 referals)
google.com : php getting url (8 referals)
www.phpfreaks.com : PHP Help: Alan Knowles' Blog: Getting Started with HTML_FlexyFramework, DataObjects and HTML_Template_Flexy (7 referals)
Follow us
-
- Some thoughts on the language server and its usefulness in the roobuilder
- Roo Builder for Gtk4 moving forward
- Clustered Web Applications - Mysql and File replication
- GitLive - Branching - Merging
- PDO_DataObject Released
- PDO_DataObject is under way
- Mass email Marketing and anti-spam - some of the how-to..
- Hydra - Recruitment done right
Blog Latest
-
Twitter - @Roojs