2.URL to Object mapping, and config loader (HTML_FlexyFramework)

HTML_FlexyFramework

This is a brief introduction to trying to get the current framework set up - I've not got alot of time at present to keep documenting it, but one day this will get finished - The framework It's self, has been tested, and used on a number of sites, and has proved to be very good in quickly developing flexible, managable, extendable sites.

Note: these documents are a little dated, most of the configuration is either auto generated or supplied to the constructor now


Setup

The core framework package is HTML_FlexyFramework and can be installed using
pear install HTML_FlexyFramework-0.6.tgz (available here)

from here on, it gets more complex!

technically I usually develop a whole site outside the web tree - for example in /home/alan/Projects/example_site, in here I install all the pear components that are needed for the site - The whole project looks pretty similar to a pear install directory! - The first trick is to change the pear install path to get it up and going..
pear config-set php_dir /home/alan/Projects/example_site
After this is done, you can install all the key components, like HTML_FlexyFramework, DB_DataObjects and HTML_Template_Flexy

Configuration and bootstrapping

since all this code is outside the main web root, you need to add a small file in the real web root, to redirect requests to your install directory. - this is done by a very small index.php file inside the web root:
<?php
ini_set('include_path','/home/alan/Projects/example_site');
require_once 'HTML/FlexyFramework.php';
$options = array(
'project' => 'Example',
);
HTML_FlexyFramework::factory($options);
?>
and thats it, from here on, the Framework takes over the job of deciding how to deal with the url that is requested

The first thing that the framework does is look for a directory ConfigData within the application directory and load a file default.ini, which contains the default settings for the application. an example of this can be found in the here ConfigData_default.ini if you are working with multiple domains, (eg. a staging and live server), you can create ini files like : staging.myhost.com.default.ini and live.myhost.com.default.ini and put these in the Config Data.

[Note] after using the framework for  a while, it is becomming apparent that you should parse_ini_file(xx,true) in the index.php and use that as the argument to factory. (add 'file'=>'.unused') as it makes it alot clearer where your configuration comes from.

Running the Pages

The next step on the process is executing the page code,  Using the Project name you set it has to decide what class to load in relation to the URL:
  • http://www.example.com/example_site/  (assuming index.php is set to be the directory index) will do a header redirect to the real url.
  • http://www.example.com/example_site/index.php
    • will load example_site/Example.php (class Example)
  • http://www.example.com/example_site/index.php/testpage.html
    •  will load   example_site/Example/testpage.php (class Example_testpage)
  • http://www.example.com/example_site/index.php/testpage/xxxx.html
    • if the file Example/testpage/xxxx.php does not exist - it will load class Example_testpage as before.
      • the first argument to start/get/post methods will be 'xxxx';
    • otherwise it will load the xxxx.php file as usual..

a sample page class would look like
<?
class Example_index extends HTML_FlexyFramework_Page {

var $template = "welcome.html";
var $masterTemplate = "master.html";
var $loadmodules = array( ); // dont load any modules

var $user; // store user here for reference

function getAuth() { // authenitcaiton lets in authenicated users

$auth = &PEAR::getStaticProperty('Auth','singleton');
if (!$auth->getAuth()) {
HTML_FlexyFramework::run('Welcome');
}
$this->user = $auth->getUser(); //
}


function get($v) {
$this->example = 'hello world';
}
function post($v) {
print_r($_POST);
}

}
?>
In this example, the first thing to notice is that the templates that this page uses are defined as $template and $masterTemplate, - the load modules is explained later (when I get time).. - this means that if this page/class displays, it will use the files from the templates folder called master.html and welcome.html to build the page.

The next step is the page's authentication check, in this example, the page check to see if the user is authenticated (you could add more complex checks here - to see if the user was a member of some group etc.), and returns login if the user is not authenticated. the pear::getStaticProperty is set by the Framework that created this class.
by returning login, the framework will call the class Example_login, in the same way this is called, rather than continuing to call the class's start method. (the start method works in the same way - if you return a string, it will try and run the class that relates to that name.

Next the start method is called (the base class defines start a call get or post depending on if the page was called with a get or post request - so you can put form processing into the post method, and for setup into the get method).

After this the output method is called, by default (in the base class, this calls HTML_FlexyFramework, and overlays this object's variables onto the template) - if you need to do any post processing or modify the output process, you can put the code in the output method, and call parent::output();

Add a comment (requires javascript!)

Name
Email
Homepage
Comment
 

Edit Document | Create Page:
Contact me at alan@akbkhome.com