PHPSimpl 0.8.0 RC1 Released

PHPSimpl 0.8.0 is feature complete and is posted on Google Code. The release candidate will be up for about a week and we will not be adding any more features or API changes. We are only concerted with bug fixed and security issues. Please download it if you are interested in giving it a try and let us know what you think. The final will be out in about a week and we are hoping to have a more feature complete example in it.

The Links

Please submit any bugs to the Issue List.

PHPSimpl 0.8.0 Beta 1 Released

The first beta PHPSimpl has been released to the public. You may be wondering why we decided to start at 0.8, well we have put together a Version History in the wiki and also a short Road Map. We have been using some form of a DbTemplate at WSU for a while now it just has not been public. Over the past two years it has continued to grow and strengthen. After submitting it to google code initially we knew it still needed some work and we took on the challenge of creating a solid framework that could be easily implemented and rapidly create applications with minimal configuration.

The Links

Remember this is just a beta, there is still some polishing to do. Although it is working this is still considered Beta software. Please submit any bugs to the Issue List.

Multiplication

The MultiForm() function is here. It allows an entire form or just one form field to be displayed with the [] brackets on the end of the input name. This allows the form to have multiple copies of the same fields in the same form without anything getting overwritten or any additional setup.

Here is how it is used:

// Create the Address
$myAddress = new Address;// Get a list of all their addresses
$myAddress->SetValue('customer_id', $myCustomer->GetPrimary());
$myAddress->GetList();// Display all their current addresses
if (count($myAddress->results) > 0){
foreach($myAddress->results as $address){
$myAddress->ResetValues();
$myAddress->SetValues($address);
$myAddress->MultiForm();
}
}

Here is the output:
Multiplication Output

A More Centered API

Its been a while but we have been working hard on an API that is going to withstand the test of time. This will make Simpl more expendable and not require as many application code level changes in the future.

These function have been added to the Form() Class:

SetDisplay($fields) -> Boolean
SetHidden($fields) -> Boolean
SetOmit($fields) -> Boolean
SetOptions($options) -> Boolean
SetConfig($config) -> Boolean

They can make the DbTemplate and Form classes a lot easier to work with. Here is an example of how to declare a class with these new functions:

class RSVP extends DbTemplate {
/**
* Class Constuctor
*
* @param $data array
* @return null
*/
function __construct($data=''){
// Setup the required fields
$required = array('first_name', 'last_name', 'email', 'phone', 'is_attending');
// Setup their labels
$labels = array('first_name' => 'First Name', 'last_name' => 'Last Name', 'email' => 'E-Mail', 'is_attending' => 'Attending');
// Setup the examples
$examples = array('email' => 'name@domain.com'); // Call the Parent Constructor
$this->DbTemplate($data, $required, $labels, $examples, 'rsvp', '', 'my_database'); // Set the Display
$display = array('first_name', 'last_name', 'email', 'phone', 'is_attending');
$this->SetDisplay($display); // Add a validation Type
$this->Set('validate','homepage', 'url'); // Set the defaults
$defaults = array('is_attending' => 1);
$this->SetDefaults($defaults); // Set the Options
$options = array('is_attending' => array('1' => 'Yes', '0' => 'No'));
$this->SetOptions($options); // Set the Config
$config = array('is_attending'=>'radio');
$this->SetConfig($config);
}
}

Now the display and the config information follow the class where ever it is used. Also the default values can be used to set up the form before the user enters any information.Example of this being used on the page:

// Create an RSVP Instance
$myRSVP = new RSVP;// Display the form
echo '<form>';
$myRSVP->Form();
echo '<input name="submit" value="Save" type="submit">';
echo '</form>';

PHP Sessions in a Load Balanced Environment

One thing that we struggle with in our server environment as many others do is doing load balancing and being able to sync session data between all the machines. Having one dedicated session space or server can be an option but that can get a little messy or expensive. So we added MySQL Sessions to the Simpl framework and it is easy as pie to use.

Originally adopted from Joseph Crawford but implemented completely in Simpl. Josephs was great except it seemed to be connected to a larger whole. The bits and pieces were there for the session but they just needed to be combined with a framework. Below is a fully featured example working and usable.

Prerequisits:

  • PHPSimpl
  • MySQL Database Connection
  • “session” Table
    CREATE TABLE `session` (
    `ses_id` varchar(32) NOT NULL,
    `last_access` int(12) unsigned NOT NULL,
    `ses_start` int(12) unsigned NOT NULL,
    `ses_value` text NOT NULL,
    PRIMARY KEY (`ses_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Used to store the sessions data';

Usage Steps:

  1. Download and copy Simpl to the web server.
  2. Define some base items
    // Directories
    // Always Include trailing slash "/" in Direcories
    define('DIR_ABS', '/usr/local/www/');
    define('FS_SIMPL', DIR_ABS . 'simpl/');
    define('FS_CACHE', DIR_ABS . 'cache/');// Enable Database Sessions
    define('DB_SESSIONS', true);// Database Connection Options
    define('DB_USER', 'username');
    define('DB_HOST', 'localhost');
    define('DB_PASS', 'password');
    define('DB_DEFAULT', 'site_data');
  3. Include the framework
    // Simpl Framework
    include_once(FS_SIMPL . 'simpl.php');
  4. Connect to the database
    // Make the DB Connection
    $db = new DB;
    $db->Connect();
  5. Use the $_SESSION super global just as you normally would and PHPSimpl does all the work for you.

The only modification may be the page cannot close the database connection since the _SESSION variable has to be written after the page is completely loaded and thus must still have the connection available.

Simpl and the $db object to all the hard work of redirecting the session data and pulling it from the database. No need to call a session_start() or even interact with the session table.

If you are interested in what the Session class looks like here is the source code:
Session.php