Skip to content

Instantly share code, notes, and snippets.

@aredridel
Created September 4, 2011 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aredridel/1192217 to your computer and use it in GitHub Desktop.
Save aredridel/1192217 to your computer and use it in GitHub Desktop.
Simple request/action/response and template example

PHP Minimal Framework

This is intended to be the simplest possible PHP framework for a request/action/response style with separate templates.

PHP is already a fantastic templating engine, so there's no need to reinvent the wheel. Templates are plain PHP.

There's no object orientation. There's no need -- that's great for a model layer, but this framework leaves that part up to you.

There's no routing. The web server does this neatly by mapping to filenames.

Each of these action files is short: include the framework functions, look at input, decide what to display.

The demo.php, templates/demo.template.php and handle_demo.php show a two-stage form handler, a great way to do things -- first one is easy, just display the form. Then when it's posted, do validation and store errors. If there's no errors, show success. If not, show the form again. In the form template, if there are errors for a field, show them.

Users only see the action files in URLs. The templates are behind the scenes.

The site layout -- things like CSS files and titles -- is in templates/layout.template.php.

<?php
require('framework.php'); // Load the framework
$title = 'Demo Form'; // Set the title
render('demo'); // Render the demo template, and implicitly, wrap it in the layout template.
<form action='handle_demo.php' method='post'>
<p><label>Field 1 <input name='field1' placeholder='Stuff here' value='<?php echo htmlentities(@$_POST['field1']); ?>'></label></p>
<p><label>Stuff Box <input name='stuff' placeholder='Stuff here' value='<?php echo htmlentities(@$_POST['stuff']) ?>'></label> Enter "Stuff" in the box.</p>
<?php if($errors['stuff']): ?>
<p class='error'><?php echo $errors['stuff']; ?></p>
<?php endif; ?>
<input type='submit'>
</form>
<p>Yay! That works! You entered:</p>
<dl>
<dt>Field 1</dt>
<dd><?php echo $_POST['field1']; ?></dd>
<dt>Stuff</dt>
<dd><?php echo $_POST['stuff']; ?></dd>
</dl>
<p>That was easy, wasn't it?</p>
<p>Go back to the <a href='index.php'>Index</a></p>
Post
+-----+
| |
+-----------+-+ | +------------+
+--------------+ | | +-v------------+ | |
| | | Register | | | | Logged in |
| | | Template | | Do | | Template |
| Register | | | | Register | | |
| | | | | | | |
| | +-------^-----+ | | +--^---------+
+-----------+--+ ^ | +-+-------+----+ |
| | | | | |
| | +---------+ +-----------+
+------+ Bad Good
Display
<?php
/**
* render is really the entire framework, other than the conceptual layout:
* It's a simple function to render a template, hold it for a moment, then
* render a layout that can include the rendered template.
*/
function render($template, $layout = 'layout') {
extract($GLOBALS); // Make global variables accessible to the template.
ob_start(); // Hold the output of the template.
// Load the template (with some filename extensions to keep things obvious,
// but so we don't have to specify in the code.)
include("templates/$template.template.php");
$content = ob_get_contents(); // Snag what was held.
ob_end_clean(); // Clean out the buffer so we don't get two copies.
// Now include the layout -- it can use $content to put the template
// output where it belongs.
include("templates/$layout.template.php");
}
<?php
require('framework.php'); // Load the framework.
$errors = array(); // Start a list of errors.
// This is a rediculous but simple validation. Write your own for your
// own forms!
if(@$_POST['stuff'] != 'Stuff')
$errors['stuff'] = "You must enter 'Stuff' in the stuff box";
if(count($errors) > 0) {
// If there's errors, show the form again
render('demo');
} else {
// Otherwise show the success page.
render('demoSuccess');
}
<?php
require('framework.php'); // Load the framework
$title = 'Demo Index Page'; // Set the page title
render('index'); // Render the index template (and implicitly, wrap it in the layout template)
<p>This is the index page!</p>
<p><a href='demo.php'>Demo page</a></p>
<html>
<head>
<?php if(@$title): ?>
<title><?php echo $title ?></title>
<?php endif; ?>
<link rel='stylesheet' href='site.css'>
</head>
<body>
<?php if(@$title): ?>
<h1><?php echo $title ?></h1>
<?php endif; ?>
<?php echo $content; ?>
</body>
</html>
Copyright (c) 2011 Aria Stewart
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
/* We use the browser's defaults styles, but for things with the error
class, highlight in red. */
.error { background-color: red; color: white; padding: 1em; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment